我已阅读本文,其中介绍了如何使用编辑器模板显示动态字段http://coding-in.net/asp-net-mvc-3-how-to-use-editortemplates/
如何通过点击“添加字段”按钮创建新字段?
我已经搜索了这个并查看了一些示例,但它们似乎过于复杂,不应该是一个简单的过程。在PHP中,您只需将[]
附加到字段名称的末尾,然后您就可以通过循环运行它......
在MVC中应该有更简单的方法,因为它是更新的技术,对吗?
答案 0 :(得分:0)
动态表单字段可以轻松完成,如下所示:
型号:
public class MyViewModel
{
public string Name { get; set; }
public List<string> EmailAddresses { get; set; }
}
查看:
@model MyViewModel
@Html.TextboxFor(m => m.Name)
@* you can add as many of these as you like, or add them via javascript *@
<input type="text" name="EmailAddresses" />
<input type="text" name="EmailAddresses" />
<input type="text" name="EmailAddresses" />
@* you can also use razor syntax *@
@Html.Textbox("EmailAddresses")
控制器:
[HttpPost]
public ActionResult MyAction(MyViewModel model)
{
// note that model.EmailAddresses contains your list of email addresses
foreach (string email in model.EmailAddresses)
email = email; // do whatever you want with this...
return View();
}