NealR
NealR

Reputation: 10689

ASP MVC view not displaying updated data when nullified

In my ASP MVC 3 site, when a user selects a value of Sole Proprietor from the Producer Type drop down box, all values inside a fieldset by the name of DSS Specific are to be nullified and no longer displayed when the view is re-rendered.

When stepping through the code I can see that after the user hits save, the model object evaluates the Producer Type correctly, sets the appropriate fields to null, and when the view is being rendered I can see that the fields in the Model object are in fact null where they need to be.

However, the view still displays the values that were in the field prior to hitting save, and does so despite the object being passed to it conatining null values for those fields. I have also hit refresh a dozen times to make sure that page was not simply cached in the browser.

CONTROLLER

When the model object is first passed back to the controller, it is run through a method called 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);

This is the code in ReformatFields that determines whether or not to nullify the specific fields. The model object is then assigned back to the agenttransmission object above in the controller.

        //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;
    }

After being saved, the agenttransmission model object is then sent back to the view

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

VIEW

As you can see from the screen shot below, the Model object being passed back to the view has a value of null for CONCode and BundleCode.

enter image description here

However, the page renders with the same values as before .

enter image description here

And it is not a problem of the page caching the values as you can see here the HTML is actually being rendered with these values

enter image description here

Here's the actual code from the view for these two fields

    <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>

I realize this is an involved process so if I'm missing anything you think would be helpful please let me know. Thank you!

Upvotes: 1

Views: 1750

Answers (1)

Dismissile
Dismissile

Reputation: 33071

When you do a POST all that data is stored in the ModelState. Setting the properties in your model to null and then returning that model in your view isn't going to blank them out in the view, because they are still in ModelState. You can either do a redirect to a GET action and persist this data in another way or clear your ModelState:

ModelState.Clear();

The Html helpers such as @Html.TextBoxFor are pulling the data from the ModelState and not directly from your model class, and that is why the values still appear even though you set them to null.

Upvotes: 3

Related Questions