如何在 ASP.NET MVC 中使用多个表单元素 [英] How to use multiple form elements in ASP.NET MVC
问题描述
所以我是 ASP.NET MVC 的新手,我想为集合中的每个项目创建一个带有文本框的视图.如何执行此操作,以及如何在 POST 回传时捕获信息?我曾使用表单和表单元素为模型构建静态表单,但从未基于可变大小集合动态生成表单元素.
我想在 mvc 3 中做这样的事情:
@foreach(Model.Guests 中的来宾){<div>名字:<br/>@Html.TextBoxFor(???) @* 我不能做 x =>x.FirstName 在这里是因为该模型是自定义类型 Invite,并且lambda 想为此公开属性类型,而不是 foreach 循环中的 Guest.*@
}
如何为每位客人做一个文本框?以及如何在它回发的操作方法中捕获它们?
感谢您的帮助.
绝对是编辑器模板的工作.所以在你看来,你把这行:
@Html.EditorFor(x => x.Guests)
并在相应的编辑器模板内(~/Views/Shared/EditorTemplates/Guest.cshtml
)
@model AppName.Models.Guest<div>名字:<br/>@Html.TextBoxFor(x => x.FirstName)
仅此而已.
现在以下操作将立即生效:
public ActionResult Index(int id){SomeViewModel 模型 = ...返回视图(模型);}[HttpPost]公共 ActionResult 索引(SomeViewModel 模型){如果 (!ModelState.IsValid){返回视图(模型);}//TODO: 用你从视图中得到的模型做一些事情return RedirectToAction("成功");}
请注意,编辑器模板的名称很重要.如果您的视图模型中的属性是:
public IEnumerable客人{得到;放;}
编辑器模板应命名为 Guest.cshtml
.它将自动为 Guests
集合的每个元素调用,它会负责正确生成输入的 id 和名称,以便在您 POST 回时一切自动工作.
结论:每次在 ASP.NET MVC 视图中编写循环(for
或 foreach
)时,您都应该知道自己做错了,并且存在更好的方法.
So I am new to ASP.NET MVC and I would like to create a view with a text box for each item in a collection. How do I do this, and how do I capture the information when it POSTs back? I have used forms and form elements to build static forms for a model, but never dynamically generated form elements based on a variable size collection.
I want to do something like this in mvc 3:
@foreach (Guest guest in Model.Guests)
{
<div>
First Name:<br />
@Html.TextBoxFor(???) @* I can't do x => x.FirstName here because
the model is of custom type Invite, and the
lambda wants to expose properties for that
type, and not the Guest in the foreach loop. *@
</div>
}
How do I do a text box for each guest? And how do I capture them in the action method that it posts back to?
Thanks for any help.
Definitely a job for an editor template. So in your view you put this single line:
@Html.EditorFor(x => x.Guests)
and inside the corresponding editor template (~/Views/Shared/EditorTemplates/Guest.cshtml
)
@model AppName.Models.Guest
<div>
First Name:<br />
@Html.TextBoxFor(x => x.FirstName)
</div>
And that's about all.
Now the following actions will work out of the box:
public ActionResult Index(int id)
{
SomeViewModel model = ...
return View(model);
}
[HttpPost]
public ActionResult Index(SomeViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
// TODO: do something with the model your got from the view
return RedirectToAction("Success");
}
Note that the name of the editor template is important. If the property in your view model is:
public IEnumerable<Guest> Guests { get; set; }
the editor template should be called Guest.cshtml
. It will automatically be invoked for each element of the Guests
collection and it will take care of properly generating ids and names of your inputs so that when you POST back everything works automatically.
Conclusion: everytime you write a loop (for
or foreach
) inside an ASP.NET MVC view you should know that you are doing it wrong and that there is a better way.
这篇关于如何在 ASP.NET MVC 中使用多个表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!