以下是我的控制器代碼:某些字段無法通過即將在ModelBinder的
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, Actor actor)
{
try
{
actorRepository.Save(actor);
return RedirectToAction("Index");
}
catch
{
return View("Edit");
}
}
調用具有一個強類型的演員類的局部視圖的視圖。出於某種原因,有幾個領域沒有被綁定到課堂上。有問題的字段是地址,地址2和ZipCode。它們在頁面上填充,但它們返回null。其他領域都在那裏,而不是這些。
任何想法?另外,我如何編寫單元測試來複制這種行爲?
更新 公共類演員 { 公共字符串中間名{獲得;設置;}
[Required(ErrorMessage = "First Name is Required")]
[RegularExpression(@"\w*", ErrorMessage = "Last Name Contains Illegal Characters")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is Required")]
[RegularExpression(@"\w*", ErrorMessage = "Last Name Contains Illegal Characters")]
public string LastName { get; set; }
[DataType(DataType.PhoneNumber, ErrorMessage = "Please Enter a Valid Phone Number")]
[Required(ErrorMessage = "Phone Number is Required")]
public string PhoneNumber { get; set; }
[Required(ErrorMessage = "Address is Required")]
[RegularExpression(@"\w*", ErrorMessage = "Address Contains Illegal Characters")]
public string Address { get; set; }
[RegularExpression(@"\w*", ErrorMessage = "Address2 Contains Illegal Characters")]
public string Address2 { get; set; }
[RegularExpression(@"\w*",ErrorMessage = "State Contains Illegal Characters")]
[Required(ErrorMessage = "State is Required")]
public string State { get; set; }
[Required(ErrorMessage = "Zip Code is Required")]
[RegularExpression("\b[0-9]{5}(?:-[0-9]{4})?\b",ErrorMessage = "Please Enter a Valid Zip Code")]
public string ZipCode { get; set; }
[Required(ErrorMessage = "Even in theater, you have to choose a gender")]
public bool? Gender { get; set; }
[Required(ErrorMessage = "Cell Phone Number is Required")]
public string CellPhone { get; set; }
public int ActorId { get; set; }
[DataType(DataType.MultilineText, ErrorMessage = "Illegal Characters in Notes")]
public string Notes { get; set; }
[Required(ErrorMessage = "Email Address is Required")]
[DataType(DataType.EmailAddress)]
public string EMail { get; set; }
[Required(ErrorMessage = "City Is Required")]
public string City {get; set;}
}
<fieldset>
<legend>Fields</legend>
<p>
<label for="MiddleName">MiddleName:</label>
<%= Html.EditorFor(m=>m.MiddleName) %>
<%= Html.ValidationMessage("MiddleName", "*") %>
</p>
<p>
<label for="FirstName">FirstName:</label>
<%=Html.EditorFor(m=>m.FirstName) %>
<%= Html.ValidationMessage("FirstName", "*") %>
</p>
<p>
<label for="LastName">LastName:</label>
<%= Html.TextBox("LastName", Model.LastName) %>
<%= Html.ValidationMessage("LastName", "*") %>
</p>
<p>
<label for="PhoneNumber">PhoneNumber:</label>
<%= Html.TextBox("PhoneNumber", Model.PhoneNumber) %>
<%= Html.ValidationMessage("PhoneNumber", "*") %>
</p>
<p>
<label for="Address">Address:</label>
<%=Html.EditorFor(m=>m.Address) %>
<%= Html.ValidationMessage("Address", "*") %>
</p>
<p>
<label for="Address2">Address2:</label>
<%=Html.EditorFor(m=>m.Address2) %>
<%= Html.ValidationMessage("Address2", "*") %>
</p>
<p>
<label for="State">State:</label>
<%= Html.TextBox("State", Model.State) %>
<%= Html.ValidationMessage("State", "*") %>
</p>
<p>
<label for="ZipCode">ZipCode:</label>
<%= Html.TextBox("ZipCode", Model.ZipCode) %>
<%= Html.ValidationMessage("ZipCode", "*") %>
</p>
<p>
<label for="Gender">Gender:</label>
<%= Html.TextBox("Gender", Model.Gender) %>
<%= Html.ValidationMessage("Gender", "*") %>
</p>
<p>
<label for="CellPhone">CellPhone:</label>
<%= Html.TextBox("CellPhone", Model.CellPhone) %>
<%= Html.ValidationMessage("CellPhone", "*") %>
</p>
<p>
<label for="ActorId">ActorId:</label>
<%= Html.TextBox("ActorId", Model.ActorId) %>
<%= Html.ValidationMessage("ActorId", "*") %>
</p>
<p>
<label for="Notes">Notes:</label>
<%= Html.TextBox("Notes", Model.Notes) %>
<%= Html.ValidationMessage("Notes", "*") %>
</p>
<p>
<label for="EMail">EMail:</label>
<%= Html.TextBox("EMail", Model.EMail) %>
<%= Html.ValidationMessage("EMail", "*") %>
</p>
<p>
<label for="City">City:</label>
<%= Html.TextBox("City", Model.City) %>
<%= Html.ValidationMessage("City", "*") %>
</p>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
你能列出你的視圖和Actor對象的定義嗎? – Rudy 2009-11-01 07:03:46
是的,演員是一個POCO課程,在視圖中沒有任何異國情調(無論如何我都知道)。 – KevDog 2009-11-01 13:07:16