在我的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
對象是傳遞迴該視圖的值爲null
的CONCode
和BundleCode
。
然而,頁面相同的值之前呈現。
它是不是緩存值,你可以在這裏看到的HTML實際上是被渲染的這些值的頁面的問題
下面是從實際的代碼查看這兩個字段
<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>
我意識到這是一個涉及的過程,所以如果我失蹤任何你認爲會有幫助的,請讓我知道。謝謝!
太棒了,感謝您的解釋! – NealR