2009-11-01 49 views
0

以下是我的控制器代碼:某些字段無法通過即將在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> 
+0

你能列出你的視圖和Actor對象的定義嗎? – Rudy 2009-11-01 07:03:46

+0

是的,演員是一個POCO課程,在視圖中沒有任何異國情調(無論如何我都知道)。 – KevDog 2009-11-01 13:07:16

回答

0

這裏是你如何測試模型聯:

How to test custom Model Binders in ASP.NET MVC?

只有你將測試DefaultModelBinder。

至於字段,你還沒有顯示演員的源代碼。但既然你談論字段,我猜你確實在使用字段 - 但ASP.NET MVC模型活頁夾不會綁定到字段 - 僅限於屬性。所以,如果你有

public class Actor 
{ 
    public string ZipCode; 
} 

你需要改變

public class Actor 
{ 
    public string ZipCode { get; set; } 
} 

另一個原因可能是,你有

public class Actor 
{ 
    public Address Address { get; set; } 
} 

,並使用部分顯示地址;所以它被命名爲「ZipCode」,但它必須命名爲「Address.ZipCode」。或者也許你的地址沒有無參數的構造函數......換句話說,沒有看到源代碼就有太多猜測。

隨着更新信息,這是無意義的。壞域看起來和好域一樣。但試試Html.TextBox而不是EditorFor。嘗試使用FireBug(或IE Dev)查看POST數據,檢查字段名稱和值是否正確。如果他們這樣做 - 問題在於模型綁定器,如果他們不好/缺席 - 頁面出現問題。此外,請嘗試檢查控制器操作中的ModelState.IsValid以及ModelState錯誤。

+0

我使用自動屬性,它只是實現方面的字段的語法糖。更多的時刻來臨,我寫這篇文章的時候已經很晚了,而且我沒有時間繼續發佈代碼。 – KevDog 2009-11-01 12:52:19

+0

這些字段最初是Html.TextBox,我將它們切換到EditorFor以查看它是否有所作爲。它沒有,並且因爲它們使用兩種方法在頁面上呈現相同的結果,所以我幾乎排除了這是一個原因。作爲一個檢查,我爲Add方法創建了一個新的強類型視圖,並獲得了相同字段的相同錯誤。 時間深入一點。 – KevDog 2009-11-01 17:30:36

2

有一個ModelMetaData屬性,名爲ConvertEmptyStringsToNull(default = true),它將ASP.MVC的DefaultModelBinder類中的空字符串轉換爲NULL。

您可以在每個屬性通過DataAnnotation改變這種像這樣:

[DisplayFormat(ConvertEmptyStringToNull=false)] 

,或者你可以創建自己的模型綁定器,並從DefaultModelBinder繼承,只是重寫GetPropertyValue方法忽略這個設置是這樣的:

ModelBinders.Binders.DefaultBinder = new Delphi.Mvc.KeepStringsBinder(); ModelBinders.Binders.Add(typeof(ObjectBase),new Delphi.Mvc)。KeepStringsBinder());

相關問題