一个ASP.带有带有动态输入数的视图的.NET MVC
An ASP.NET MVC with a view with a dynamic number of inputs
好吧,这是一个复杂的问题,我是新手,也许我甚至不知道如何正确地问。
我正在使用ASP构建一个应用程序。净MVC。我有一个视图,其中包含一个表单,该表单有一个文本框输入,要求用户将接收者命名为遗嘱的遗产。在输入下面,我想要一个按钮("添加另一个收件人"),用于向表单添加另一个输入。
下面是我如何使用Javascript模拟这一点(点击"是",然后点击"现金",看看我在说什么):https://jsfiddle.net/cinerobert/409k27ot/7/
<p>Would you like to leave specific gift in your will?</p>
<p>
<button onclick="q1AnswerYes()">Yes</button>
<button onclick="q1AnswerNo()">No</button>
</p>
<br />
<!-- If answer to Q1 is Yes show this block -->
<div id="q1AnswerYesBlock" style="display:none; text-align:center;
margin:auto;">
<div>What specific gifts would you like to leave?</div>
<hr>
<div>
<button onclick="q2AnswerCash('q2AnswerCashDisp')">Cash</button>
<button>Car</button>
<button>Real Estate</button>
<button>Jewelry</button>
<button>Other</button>
</div>
<div id="q2AnswerCashDisp">
<!-- Recipient forms get added here -->
</div>
<div style="text-align:center;
margin:auto; display:inline">
<button id="addCashRecipient" style="display:none" onclick="addCashRecipientForm('q2AnswerCashDisp')">Add another recipient</button>
</div>
</div>
<!-- If answer to Q1 is No show this block -->
<div id="q1AnswerNoBlock" style="display:none">
<p>Some other crap</p>
</div>
<div id="testFormHTML" style="display:none">
<form id="testFormId" method="get" style="border-syle:none">
<fieldset id="fieldsetid" style="border-style:none">
<div class="formPrompt">
Amount:
</div>
<input type="text">
<div class="formPrompt">
Recipient:
</div>
<input onblur="submit()" type="text" name="recipient">
<input style="display:none" class="testFormParamInputId" name="testFormParam" value="">
<br>
<br>
</fieldset>
</form>
</div>
<script>
var cashRecipientNum = 1;
function q1AnswerYes() {
document.getElementById("q1AnswerYesBlock").style.display = "block";
document.getElementById("q1AnswerNoBlock").style.display = "none";
}
function q1AnswerNo() {
document.getElementById("q1AnswerYesBlock").style.display = "none";
document.getElementById("q1AnswerNoBlock").style.display = "block";
}
function q2AnswerCash(blockId) {
if (cashRecipientNum == 1) {
addForm(blockId, "testForm", cashRecipientNum++);
document.getElementById("addCashRecipient").style = "display:inline";
};
}
function addCashRecipientForm(blockId) {
addForm(blockId, "testForm", cashRecipientNum++);
}
// Function to add a new form with unique action argument to the document.
function addForm(blockId, formArg, numInstance) {
var formHTML = formArg + "HTML";
var formId = formArg + "Id";
var formParamInputId = formArg + "ParamInputId";
var newFormId = formArg + "Id" + numInstance;
var div = document.getElementById(formId),
clone = div.cloneNode(true); // true means clone all childNodes and all eventhandlers
clone.id = newFormId;
clone.style = "border-style:none";
document.getElementById(blockId).appendChild(clone);
var x = document.getElementById(newFormId).getElementsByClassName(formParamInputId);
x[0].value = numInstance;
console.log(x[0].class);
console.log("here");
}
</script>
我不明白的是:
因为我使用硬编码的html表单(使用标签)而不是html助手(如@Html.TextBoxFor),每个输入都不绑定到模型中的任何东西。我可以使用FormCollection读取输入,但是当表单提交时,我不知道如何使用用户输入的值重新发布表单。
如果我使用html helper (@Html.TextBoxFor)并将每个输入绑定到模型中的属性,那么我不明白如何允许表单添加无限数量的输入字段
我已经搜索了动态视图的例子,但我发现的例子都与响应模型变化的视图有关。我还没有发现一个涉及到根据用户操作添加无限数量的输入字段。
我知道这个问题有点难懂,但如果有人能帮我指出正确的方向,我将非常感激。提前感谢你对一个新手的耐心。你所描述的场景是视图模型。即使像这样简单的一个也会有帮助
public class WillRecipientsViewModel
{
public List<string> Recipients { get; set; }
}
那么你的控制器将需要你的表单的相应动作
// Controller
public ActionResult SetWillRecipients()
{
// Added this to force at least 1 text box into the view
WillRecipientsViewModel model = new WillRecipientsViewModel
{
Recipients = new List<string> { "" };
};
return View(model);
}
[HttpPost]
public ActionResult SetWillRecipients(WillRecipientsViewModel model)
{
// Business logic
// Go back to the view if an error occurs
return View(model);
}
// View
@model WillRecipientsViewModel
@using(Html.BeginForm("SetWillRecipients"))
{
@Html.DisplayFor(model => model.Recipients)
for (int i = 0; i < Model.Recipients.Count; i++)
{
// Generate the textboxes for any values that were previously submitted
Html.InputFor(model => model.Recipients[i])
}
}
这会将表单中的值绑定到Model对象,这将使事情变得更容易。然后,您只需要确保您的视图使用<input type="text" name="Recipients" />
来捕获您的帖子中的model
对象中的数据。我还建议尽最大努力使input
名称属性与视图模型的外壳紧密匹配,以避免冲突
我还包含了一些代码来举例说明如何将以前的收件人拉入接口
- 在不刷新整个网格的情况下,向Asp.net网格视图添加新行
- asp.net MVC,重定向到视图,视图打开新窗口到外部url,它'It’’’’我们被当成一种风景
- 使用javascript在MVC视图中解析.net JSON字符串
- ASP.NET angularjs重定向到控制器的另一个视图
- asp.net网格视图中的面板-我如何在jquery中找到它
- 在 MVC 中将 HTML 表单数据从视图传递到控制器 ASP.NET
- jquery专注于asp.net mvc部分视图中的一个文本框
- 在创建视图mvc.net的过程中使用javascript
- 模式视图重新加载内容(启动MVC ASP.NET)
- 如何在网格视图中验证asp.net文本框
- ASP.NET MVC 4-部分视图和Ajax.Actionlink
- 如何让javascript在Asp.Net中注入到我的布局中的视图中进行渲染
- 从ASP.NET MVC部分视图中卸载/删除javascript函数
- 是否可以在没有视图状态和回发的情况下使用asp.net web表单
- 在asp.net mvc 3中,如何在不刷新页面的情况下从部分视图获取值到父视图
- 在ASP.NET MVC3中将多个视图组合在一起
- 如果在ASP.Net MVC视图中验证失败,请设置httppostedfilebase
- net树视图,从JavaScript中获取值
- 使用jquery在asp.net表单视图中隐藏按钮
- 在ASP中以幻灯片形式显示不同的视图.NET MVC