Reputation: 1433
I've got an entity model with nested class. FSRHire is the parent and Employee is the child:
class FSRHire {
...
public virtual Employee Employee
...
}
class Employee {
...
public string LastName {get;set;}
public DateTime DOB {get;set;}
...
}
View:
<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>
The post into the controller doesn't work. Here is the data from the request:
...Employee.LastName=Worker&Employee_DOB=01%2F01%2F1970& ...
The model binder picks up the name just fine. The Employee.DOB is null.
It's probably caused by the underscore in the DOB field. (Employee_DOB). I'm wondering why it's like that and how I can fix it.
Upvotes: 0
Views: 124
Reputation: 150253
It should be in this format:
...Employee.LastName=Worker&Employee.DOB=01%2F01%2F1970& ...
Changed from:
Employee_DOB=01%2F01%2F1970&
// ^
To
Employee.DOB=01%2F01%2F1970&
// ^
Regarding to the View:
@Html.TextBox(Html.IdFor(model => model.Employee.DOB).ToString(), Model.Employee.DOB.HasValue ? Model.Employee.DOB.Value.ToString("d") : "", new { @class = "date" })
There is no IdFor
Method in the Web.Mvc dll, you probably wrote it yourself or used some library, anyway it's not working right.
Change the line in the view to look like this:
@Html.TextBoxFor(model => model.Employee.DOB, Model.Employee.DOB.HasValue ? Model.Employee.DOB.Value.ToString("d") : "", new { @class = "date" })
Upvotes: 1