我有一个嵌套类的实体模型。 FSRHire是父母,员工是孩子:
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为空。
这可能是由
答案 0 :(得分: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" })