2014-12-09 104 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