ModelBinder 在提交时未正确绑定到列表
我正在尝试正确设置复选框的 HTML,以便它们可以绑定到列表属性,但似乎无法成功执行此操作。在下面的视图代码中,我有五个复选框。仅当我至少选中第一个复选框时,它才会绑定。如果我选中除第一个之外的所有四个复选框,则不会绑定任何内容。
这是我的代码:
public class People
{
public List<Person> Crew { get; set; }
public string TeamName { get; set; }
public List<string> StatesLiveIn { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
视图:
@model TestBinder.Models.People
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.TeamName);
<h2>Crew #0</h2>
<h3>FirstName:</h3>
@Html.TextBoxFor(m => m.Crew[0].FirstName); <br />
<h3>LastName: </h3>
@Html.TextBoxFor(m => m.Crew[0].LastName);<br />
<h3>Age: </h3>
@Html.TextBoxFor(m => m.Crew[0].Age);<br />
<h3>States Lived In: </h3>
int i = 0;
foreach (string state in new List<string>() { "NY","NJ","FL","IL","TX" })
{
@state <input checked="checked" type="checkbox" value="@state" name="StatesLiveIn[@i]" /><br />
i++;
}
<input type="submit" value="submit" />
}
控制器:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(People p)
{
return View(p);
}
谢谢。
I'm trying to set up the HTML for the checkboxes correctly so they can bind to a list property, but can't seem to do so successfully. In the view code below I have five checkboxes. It only binds when I check at least the first checkbox. If I check all four checkboxes except the first one, nothing gets bound.
This is the code that I have:
public class People
{
public List<Person> Crew { get; set; }
public string TeamName { get; set; }
public List<string> StatesLiveIn { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
View:
@model TestBinder.Models.People
@{
ViewBag.Title = "Home Page";
}
@using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.TeamName);
<h2>Crew #0</h2>
<h3>FirstName:</h3>
@Html.TextBoxFor(m => m.Crew[0].FirstName); <br />
<h3>LastName: </h3>
@Html.TextBoxFor(m => m.Crew[0].LastName);<br />
<h3>Age: </h3>
@Html.TextBoxFor(m => m.Crew[0].Age);<br />
<h3>States Lived In: </h3>
int i = 0;
foreach (string state in new List<string>() { "NY","NJ","FL","IL","TX" })
{
@state <input checked="checked" type="checkbox" value="@state" name="StatesLiveIn[@i]" /><br />
i++;
}
<input type="submit" value="submit" />
}
Controller:
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(People p)
{
return View(p);
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试为您的复选框指定相同的名称:
但更好的选择是使用视图模型、编辑器模板和强类型
CheckBoxFor
帮助程序,因为您已经对checked="checked"
进行了硬编码> 属性,如果存在验证错误并且您在 POST 操作中呈现相同的视图,则用户将丢失其选择。Try giving your checkboxes the same name:
But a better alternative is to use a view model, editor templates and the strongly typed
CheckBoxFor
helper because you have hardcoded thechecked="checked"
attribute and if there are validation errors and you render the same view in the POST action the user will lose its selections.