【发布时间】:2013-06-11 18:26:42
【问题描述】:
在我的 ASP MVC 3 站点中,当用户从 Producer Type 下拉框中选择值 Sole Proprietor 时,名称为 DSS Specific 的字段集中的所有值都将被取消并且不再显示视图被重新渲染。
当单步执行代码时,我可以看到,在用户点击save 后,模型对象会正确评估Producer Type,将适当的字段设置为null,并且在呈现视图时我可以看到Model 对象中的字段实际上是 null 它们需要的位置。
但是,视图仍然显示在点击 save 之前字段中的值,尽管传递给它的对象包含这些字段的 null 值,但仍会显示。我还点击了十几次刷新,以确保该页面不是简单地缓存在浏览器中。
控制器
当模型对象第一次传回控制器时,它通过一个名为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);
}
查看
从下面的屏幕截图中可以看到,被传递回视图的Model 对象对于CONCode 和BundleCode 的值为null。
但是,页面呈现与之前相同的值。
这不是页面缓存值的问题,正如您在此处看到的那样,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>
我知道这是一个复杂的过程,所以如果我遗漏了您认为对我有帮助的任何内容,请告诉我。谢谢!
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor