2011-06-20 26 views
1

我正在使用我的第一個MVC Web應用程序(使用Razor和C#),並且我運行了一個奇怪的行爲。Html.TextBoxFor不使用新模型對象的值

我正在編輯數據的「行」並使用ajax調用來提交和重新顯示數據。就改變現有數據並存儲它而言,一切正常。另外如果我只是重新顯示提交的「行」沒有問題。

但是,我想顯示一個「新」行,其中一些值保留舊行,其餘消隱。

但是,當我將新行對象提交到部分視圖時,「空白」的值不會被@Html .... helpers拾取。但是,如果我直接顯示模型的屬性,它具有正確的(空白)值。

這裏是我的代碼的相關部分: 控制器的方法: [HttpPost] 公共的ActionResult EditLineForm(SkuRequestLine LN) { SkuRequestLine換行符= NULL;

 try 
     { 
      if (ln.Store(true)) 
      { 
       ViewData["prodcatdesc"] = DataConnection.GetProductCategory(ln.Category).description; 
       newline = new SkuRequestLine(); 
       newline.Copy(ln); 
       newline.Line = DataConnection.NextSkuRequestLineNumber(ln.Request); 
       newline.Comments = ""; 
       newline.Description = ""; 
       newline.Vendorsku = ""; 
       return PartialView("EditLineForm", newline); // this line is being executed. 
      } 
      else 
      { 
       return PartialView("EditLineForm", ln); 
      } 
     } 
     catch (Exception ex) 
     { 
      List<string> msgs = new List<string>(); 
      while (ex != null) 
      { 
       msgs.Add(ex.Message); 
       ex = ex.InnerException;     
      } 
      return PartialView("EditLineForm", ln); 
     } 
    } 

剃刀代碼:

@model Sku_Management.Models.SkuRequestLine 

@using (Ajax.BeginForm("EditLineForm", "SkuRequest", new AjaxOptions { OnSuccess = "UpdateLineList" })) 
{ 
. 
. 
. 
<tr> 
    <td> 
     <span class="editor-label"> 
      @Html.LabelFor(model => model.Description) 
     </span> 
    </td> 
    <td colspan="5"> 
     <span class="editor-field"> 
      @Html.TextBoxFor(model => model.Description, new { @class = "fortywide" }) // Displays the Description from the edited Line passed in. Not what what Model.Description is. 
      @Html.ValidationMessageFor(model => model.Description) 
     </span> 
     <span>|@Model.Description|</span> // Displays "||" which is what it should be since Model.Description is blank. 
    </td> 
</tr> 

我能想到的唯一的事情就是模型=> model.Description使用模型不是新模型傳遞到PartialView調用的緩存版本。

我已經花了一整天在網上搜索任何類似的東西,但我找不到任何甚至開始描述這種行爲。

有沒有其他人遇到過這種情況,並知道我是東錯了?

感謝

回答

2

這是因爲HTMLHelpers看向ModelState中的值使用的模型前。

您必須清除ModelState條目才能使其工作。

+0

謝謝。那正是我所錯過的。 – Richard

相關問題