【问题标题】:List property inside a model is coming null while posting the model to controller将模型发布到控制器时,模型内的列表属性变为空
【发布时间】:2014-08-17 22:04:51
【问题描述】:

我正在创建一个基本的 mvc3 应用程序,在该应用程序中,我尝试将具有列表属性(模型类型)的复杂模型类型发布到我的控制器。但是当我这样做时,该属性在模型中将变为 null。

我的模型如下:

public class EmployeeList
{
    public List<Employee> employeeList { get; set; }
}

Employee 又是一个模型:

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}

控制器代码是:

[HttpGet]
public ActionResult Index()
{
    EmployeeList em = new EmployeeList();
    em.employeeList = new List<Employee>() { new Employee(){}};
    return View(em);
}

[HttpPost]
public ActionResult Index([Bind(Prefix = "employeeList")]EmployeeList employeeList1)
{
    .......
}

观点如下:

Index.cshtml:-

@model test.Models.EmployeeList
@{
    ViewBag.Title = "Index";
 }

<h2>Index</h2>

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "testForm" }))
{
    @Html.LabelFor(m => m.eid)
    @Html.EditorFor(m => m.eid)
    @Html.HiddenFor(m => m.eid)
    <br />
    @Html.EditorFor(x => x.employeeList)
    <br />
    <p>
        <input type="submit" value="Post" />
    </p>
}

而编辑器模板视图是:

@model test.Models.Employee

@Html.LabelFor(model => model.Id)
@Html.TextBoxFor(model => model.Id)
@Html.HiddenFor(model => model.Id)
<br />
<br />
@Html.LabelFor(model => model.Name)
@Html.TextBoxFor(model => model.Name)
@Html.HiddenFor(model => model.Name)

在我检查的浏览器上,名称和 id 的生成如下:

name="employeeList[0].Id", id="employeeList_0__Id"

name="employeeList[0].Name", id="employeeList_0__Name"

但是当我将此 EmployeeList 模型发布到控制器时,我得到的 employeeList 为 null。

如果我遗漏了什么,请帮助我。 提前致谢。

【问题讨论】:

    标签: c# asp.net-mvc-3


    【解决方案1】:

    我认为 mvc 模型绑定可能会变得混乱。尝试将模型属性的名称从employeeList 更改为ListOfEmployees。例如

    public class EmployeeList
    {
        public List<Employee> ListOfEmployees { get; set; }
    }
    
    [HttpPost]
    public ActionResult Index([Bind(Prefix = "ListOfEmployees")]EmployeeList employeeList1)
    {
        .......
    }
    
    @model test.Models.EmployeeList
    @{
        ViewBag.Title = "Index";
     }
    
    <h2>Index</h2>
    
    @using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "testForm" }))
    {
        @Html.LabelFor(m => m.eid)
        @Html.EditorFor(m => m.eid)
        @Html.HiddenFor(m => m.eid)
        <br />
        @Html.EditorFor(x => x.ListOfEmployees)
        <br />
        <p>
            <input type="submit" value="Post" />
        </p>
    }
    

    【讨论】:

    • 我尝试更改属性employeeList的名称但得到相同的结果...@Teppic
    • 我还有什么遗漏吗?
    • 您是否也更新了 Bind 属性的 Prefix 值?即公共 ActionResult Index([Bind(Prefix = "ListOfEmployees")]EmployeeList employeeList1)
    • 是的,我也更改了前缀,但得到了相同的结果,列表仍然是空的...@Teppic
    • 您可以尝试从 Index 操作中删除 Bind 属性并再试一次吗?即公共 ActionResult Index(EmployeeList employeeList1)
    【解决方案2】:

    正如您所发现的,您需要删除BindAttribute。解释一下:

    当您使用[Bind(Prefix="employeeList")] 时,您是在告诉ModelBinder 从回发的每个属性名称的开头去除“employeeList”,而不是employeeList[0].IdemployeeList[0].Name(正确为你回复class EmployeeList)你得到[0].Id[0].Name(但class EmployeeList没有属性IdName

    如果您按原样使用该属性,则需要将参数更改为

    public ActionResult Index([Bind(Prefix = "employeeList")]List<Employee> employeeList1)
    

    会正确绑定

    【讨论】:

      猜你喜欢
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      相关资源
      最近更新 更多