2014-12-09 80 views
2

我正面臨這個奇怪的問題,無法理解它,我有一個表單接受人員ID,然後從API讀取數據並填充用戶界面編輯目的。模型在提交到服務器後保留它的值

enter image description here

下面是這種形式的標記,我猜它有事情做與模型綁定,因爲我有兩個表單標籤,兩者具有相同的模型ID。

@using (Html.BeginForm("UpdatePerson", "Person", FormMethod.Get)) 
 
{ 
 
    <table> 
 
     <tr> 
 
      <td colspan="2"> 
 
       <h3>Read Person for Edit</h3> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label>@Html.LabelFor(m => m.Id)</label> 
 
      </td> 
 
      <td> 
 
       @Html.TextBoxFor(m => m.Id) 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td colspan="2"> 
 
       <input type="submit" name="btnReadPerson" value="Read Person" /> 
 
      </td> 
 
     </tr> 
 
    </table> 
 
} 
 
@using (Html.BeginForm("UpdatePerson", "Person", FormMethod.Post)) 
 
{ 
 
    <table> 
 
     <tr> 
 
      <td> 
 
       <label>@Html.LabelFor(m => m.Id)</label> 
 
      </td> 
 
      <td> 
 
       @Html.TextBoxFor(m => m.Id, new { @readonly = "readonly" }) 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <label>@Html.LabelFor(m => m.Type)</label> 
 
      </td> 
 
      <td> 
 
       @Html.TextBoxFor(m => m.Type) 
 
      </td> 
 
     </tr>
我已經剝離的觀點,我試圖保持它簡單。

下面是處理的行動獲得

[HttpGet] 
[ActionName("UpdatePerson")] 
public ActionResult UpdatePersonRead(PersonEditModel model) 
    { 
     if (model.Id.HasValue) 
     { 
      var apiClient = new ApiClient (ApiVersions.v1); 
      var segmentReplaceList = new Dictionary<string, string> { { "{id}", model.Id.Value.ToString() } }; 
      bool ApiHitStatus = false; 
      var result = apiClient.MakeAPIRequest(out ApiHitStatus, ResourceUriKey.Person, segmentReplaceList, HttpVerbs.Get, string.Empty); 

      model = new PersonEditModel(); 
      if (ApiHitStatus) 
      { 
       var personToBeUpdated = JsonConvert.DeserializeObject<RootChildPerson>(result); 
       if (personToBeUpdated != null)//Assigning json obj to model 
       { 
        model.NameFirst = personToBeUpdated.name_first; 
        model.NameLast = personToBeUpdated.name_last; 
        model.NameMiddle = personToBeUpdated.name_middle; 
        model.SocialSecurityNumber = personToBeUpdated.social_security_number; 
        model.SubType = PersonHelper.SubTypeValue(personToBeUpdated.sub_type); 
        model.Type = "person"; 
        model.DateOfBirth = personToBeUpdated.date_of_birth; 
        model.Id = personToBeUpdated.id; 
       } 
      } 
     } 

     return View(model); 
    } 

現在,因爲人物ID 4不對應於任何人,所以我得到空的JSON對象,其在轉換爲C#類導致一個空的(不因爲它的每個屬性設置爲null或空)personToBeUpdated對象,然後分配給模型,我檢查了model.Id在Controller中甚至在View中變爲null,但它不知何故賦值爲4的輸入值爲空)到兩個Person Id文本框。 請讓我知道這裏發生了什麼。

+1

值兩者中'ModelState'優先於模型值。在嘗試更新模型屬性並返回視圖之前,您需要使用'ModelState.Clear();'清除'ModelState'。 [參考這個答案](http://stackoverflow.com/questions/26654862/textboxfor-displaying-initial-value-not-the-value-updated-from-code/26664111#26664111)的行爲更詳細的解釋。 – 2014-12-09 12:11:37

+0

@StephenMuecke謝謝 – 2014-12-09 12:28:27

回答

1

以及@StephenMuecke的評論,所以我在更新之前清除了模型。

model = new PersonEditModel(); 
ModelState.Clear(); 

其還有趣地注意到,從視圖的ModelState取數據,而不是當前指定的模型的,

HtmlHelpers控件(如.TextBoxFor()等等)不結合在回發的值進行建模,而是直接從ModelState的POST緩衝區中獲取它們的值。

ASP.NET MVC Postbacks and HtmlHelper Controls ignoring Model Changes