In one of my strongly typed Views, of type ProfileModel, I invoke an editor for another type:
@model MVC3App.WebUI.Models.ProfileModel
<div class="contact-form">
@Html.EditorFor(x => x.ContactModel)
</div>
I have this simple model:
public class ContactModel
{
[Required]
[Display(Name = "Name:")]
public string Name { get; set; }
[Required]
[Display(Name = "Email:")]
public string Email { get; set; }
[Required]
[Display(Name = "Message:")]
public string Message { get; set; }
}
And this is what I use in my EditorTemplate view for ContactModel to display the textbox:
@model MVC3App.WebUI.Models.ContactModel
@Html.TextBoxFor(model => model.Name, new { placeholder = "* Name" })
The generated HTML:
<input data-val="true"
data-val-required="The field Name: is required."
id="ContactModel_Name"
name="ContactModel.Name"
placeholder="* Name" type="text" value="">
When POSTed back to the server the value is not binding to the model properties.
[HttpPost]
public ActionResult SendPatientContact(ContactModel model)
{
if (ModelState.IsValid)
{
// model.Name is null. :S
return RedirectToAction("ThankYou");
}
return RedirectToAction("ThankYou");
}
If I manually set the name of the textbox to name="Name", the value is binding correctly to the model property. Any suggestions?