type の強く型付けされたビューの 1 つで、ProfileModel
別の型のエディターを呼び出します。
@model MVC3App.WebUI.Models.ProfileModel
<div class="contact-form">
@Html.EditorFor(x => x.ContactModel)
</div>
私はこの単純なモデルを持っています:
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; }
}
ContactModel
これは、テキストボックスを表示するために EditorTemplate ビューで使用するものです。
@model MVC3App.WebUI.Models.ContactModel
@Html.TextBoxFor(model => model.Name, new { placeholder = "* Name" })
生成された HTML:
<input data-val="true"
data-val-required="The field Name: is required."
id="ContactModel_Name"
name="ContactModel.Name"
placeholder="* Name" type="text" value="">
サーバーに POST された場合、値はモデル プロパティにバインドされません。
[HttpPost]
public ActionResult SendPatientContact(ContactModel model)
{
if (ModelState.IsValid)
{
// model.Name is null. :S
return RedirectToAction("ThankYou");
}
return RedirectToAction("ThankYou");
}
name
テキスト ボックスの を手動で に設定するとname="Name"
、値がモデル プロパティに正しくバインドされます。助言がありますか?