0

我有一个带有嵌套类的实体模型。FSRHire 是父级,Employee 是子级:

class FSRHire {
    ...
    public virtual Employee Employee
    ...
}
class Employee {
    ...
    public string LastName {get;set;}
    public DateTime DOB {get;set;}
    ...
}

看法:

<div class="editor-label">
    @Html.LabelFor(model => model.Employee.LastName)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Employee.LastName)
    @Html.ValidationMessageFor(model => model.Employee.LastName)
</div>
<div class="editor-label">
    @Html.LabelFor(model => model.Employee.DOB)
</div>
<div class="editor-field">
    @Html.TextBox(Html.IdFor(model => model.Employee.DOB).ToString(), Model.Employee.DOB.HasValue ? Model.Employee.DOB.Value.ToString("d") : "", new { @class = "date" })
    @Html.ValidationMessageFor(model => model.Employee.DOB)
</div>

进入控制器的帖子不起作用。以下是请求中的数据:

...Employee.LastName=工人&Employee_DOB=01%2F01%2F1970& ...

模型活页夹很好地取了这个名字。Employee.DOB 为空。

可能是由 DOB 字段中的下划线引起的。(Employee_DOB)。我想知道为什么会这样以及如何解决它。

4

1 回答 1

1

它应该是这种格式:

...Employee.LastName=Worker&Employee.DOB=01%2F01%2F1970& ...

更改自:

Employee_DOB=01%2F01%2F1970&
//      ^

Employee.DOB=01%2F01%2F1970&
//      ^

关于视图:

@Html.TextBox(Html.IdFor(model => model.Employee.DOB).ToString(), Model.Employee.DOB.HasValue ? Model.Employee.DOB.Value.ToString("d") : "", new { @class = "date" })

Web.Mvc dll中没有IdFor方法,您可能自己编写或使用了某个库,无论如何它无法正常工作。

将视图中的行更改为如下所示:

 @Html.TextBoxFor(model => model.Employee.DOB, Model.Employee.DOB.HasValue ? Model.Employee.DOB.Value.ToString("d") : "", new { @class = "date" })
于 2012-05-14T16:53:38.520 回答