[英]TextBoxFor setting name of property incorrectly and in turn not binding the model
In one of my strongly typed Views, of type ProfileModel
, I invoke an editor for another type: 在我的一个类型为
ProfileModel
强类型视图中,我调用了另一种类型的编辑器:
@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: 这就是我在EditorTemplate视图中用于
ContactModel
来显示文本框的内容:
@model MVC3App.WebUI.Models.ContactModel
@Html.TextBoxFor(model => model.Name, new { placeholder = "* Name" })
The generated HTML: 生成的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. POST回服务器后,该值未绑定到模型属性。
[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. 如果我手动将文本框的
name
设置为name="Name"
,则该值将正确绑定到模型属性。 Any suggestions? 有什么建议么?
在您的情况下,您不应使用@Html.EditorFor
方法,而应使用@{Html.RenderPartial("_ContactPartialView", Model.ContactModel);}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.