2013-06-11 37 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

相关问题