2013-06-11 36 views
1

在我的ASP MVC 3站點中,當用戶從Producer Type下拉框中選擇Sole Proprietor的值時,字段集中名稱爲DSS Specific的所有值都將被取消,視圖重新呈現時更長時間顯示。ASP MVC視圖在無效時不顯示更新的數據

當通過代碼步進我可以看到,用戶點擊save後,模型對象正確地評估Producer Type,設置適當的字段null,並且當視圖正在呈現我可以看到,在Model字段對象實際上是null他們需要的地方。

但是,該視圖仍然會顯示在打到save之前的字段中的值,並且儘管將對象傳遞給它,但仍然會爲這些字段值爲null值。我還打了十幾次,以確保頁面不會簡單地緩存在瀏覽器中。

CONTROLLER

當模型對象首先被傳遞迴到控制器,它通過一個所謂的ReformatFields

[HttpPost] 
    public ActionResult Edit(Monet.Models.AgentTransmission agenttransmission) 
    { 
     //redirect if security is not met. 
     if (!Security.IsModifier(User)) return RedirectToAction("Message", "Home", new { id = 1 }); 

     //Tax Id security 
     ViewBag.FullSSN = Security.IsFullSsn(User); 

     try 
     { 
      agenttransmission = AgentTransmissionValidator.ReformatFields(agenttransmission); 

方法運行時,這是在ReformatFields,其判斷是否爲不將碼取消特定字段。然後將模型對象分配回控制器中上面的agenttransmission對象。

 //TODO: blank out DSS specific fields if not a sole proprietor! 
     if (!agt.ProducerType.Equals("S") || !agt.DRMrole.Equals("Sole Proprietor")) 
     { 
      agt.C8CharAgentCAB0State = null; 
      agt.C8CharAgentCAB0MktDiv = null; 
      agt.CONCode = null; 
      agt.BundleCode = null; 
      agt.LifeRegion = null; 
      agt.LifeField = null; 
      agt.AGYMGMT = null; 
      agt.INDSGC = null; 
      agt.PENSGC = null; 
      agt.GRPSGC = null; 
      agt.LMKSGC = null; 
      agt.LDT = null; 
      agt.GCASBNS = null; 
      agt.GCOMMSN = null; 
      agt.GPROFBN = null; 
      agt.INDSplits = null; 
      agt.DSTSGC = null; 
      agt.ANNCommCode = null; 
      agt.LifeEAPercent = null; 
      agt.VARPercent = null; 
      agt.OwnerMaster = null; 
      agt.LifeMaster = null; 
      agt.CampaignMaster = null; 
      agt.RelationshipEffDate = null; 
      agt.RelationshipDistCode = null; 
     } 
     return agt; 
    } 

之後被保存,該agenttransmission模型的對象又被送回視圖

   db.SaveChanges(); 
       DisplayPrep(); 
       agenttransmission.displayChannel = getDisplayChannel(agenttransmission); 
       agenttransmission.FormattedReferenceNumber = 
        ReferenceConversion.UnobAndFormat(agenttransmission.ReferenceNumber, "S"); 
       return View(agenttransmission); 
      } 

VIEW

正如你可以從下面的截圖中看到,該Model對象是傳遞迴該視圖的值爲nullCONCodeBundleCode

enter image description here

然而,頁面相同的值之前呈現。

enter image description here

它是不是緩存值,你可以在這裏看到的HTML實際上是被渲染的這些值的頁面的問題

enter image description here

下面是從實際的代碼查看這兩個字段

<div class="M-editor-label"> 
     @Html.LabelFor(model => model.CONCode) 
    </div> 
    <div class="M-editor-field"> 
     @Html.TextBoxFor(model => model.CONCode, new { maxlength = 2 }) 
     @Html.ValidationMessageFor(model => model.CONCode) 
    </div> 

    <div class="M-editor-label"> 
     @Html.LabelFor(model => model.BundleCode) 
    </div> 
    <div class="M-editor-field"> 
     @Html.TextBoxFor(model => model.BundleCode, new { maxlength = 6 }) 
    </div> 

我意識到這是一個涉及的過程,所以如果我失蹤任何你認爲會有幫助的,請讓我知道。謝謝!

回答

2

當您執行POST時,所有數據都存儲在ModelState中。將模型中的屬性設置爲null,然後在視圖中返回該模型不會在視圖中將其刪除,因爲它們仍位於ModelState。你可以做一個重定向到一個GET行動,以另一種方式保留此數據或清除您的ModelState:

ModelState.Clear(); 

的HTML輔助,例如@Html.TextBoxFor從ModelState中從模型類拉低數據並沒有直接,這就是爲什麼即使您將它們設置爲null,值仍然顯示的原因。

+0

太棒了,感謝您的解釋! – NealR

相關問題