ModelBinder 在提交时未正确绑定到列表

发布于 2024-12-23 03:08:41 字数 1562 浏览 2 评论 0原文

我正在尝试正确设置复选框的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-12-30 03:08:41

尝试为您的复选框指定相同的名称:

<h3>States Lived In: </h3>
foreach (var state in new[] { "NY", "NJ", "FL", "IL", "TX" })
{     
    @state 
    <input checked="checked" type="checkbox" value="@state" name="StatesLiveIn" />
    <br />  
}

但更好的选择是使用视图模型、编辑器模板和强类型 CheckBoxFor 帮助程序,因为您已经对 checked="checked" 进行了硬编码> 属性,如果存在验证错误并且您在 POST 操作中呈现相同的视图,则用户将丢失其选择。

Try giving your checkboxes the same name:

<h3>States Lived In: </h3>
foreach (var state in new[] { "NY", "NJ", "FL", "IL", "TX" })
{     
    @state 
    <input checked="checked" type="checkbox" value="@state" name="StatesLiveIn" />
    <br />  
}

But a better alternative is to use a view model, editor templates and the strongly typed CheckBoxFor helper because you have hardcoded the checked="checked" attribute and if there are validation errors and you render the same view in the POST action the user will lose its selections.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文