?
Solved

ViewModel data appears to be caching in Razor View

Posted on 2016-09-14
2
Medium Priority
?
83 Views
Last Modified: 2016-09-19
I have a web app that uses C# and Razor Views.  The flow of the function is as follows:

1. User selects New Hire Class from list and clicks on Select button (see first screen in attached file)
2. Any Ids or Flags assigned to the class are displayed and an Assign Ids / Flags button is displayed (see second screen in attached file)

Here is my problem
3. If the user selects a different New Hire Class from the dropdown and clicks select, the check boxes do not get updated with the data from the new selection (see third screen in attached file - the 1st check box  is checked and it shouldn't)

Through debugging, I have verified that the model being sent to the view is correct.

Here is my Controller Code:
        public ViewResult AssignIdsFlagsToClass()
        {
            _presentationLogic.SetAssignIdsFlagsToClassViewModel();
            return View(_presentationLogic.GetAssignIdsFlagsToClassViewModel());
        }

        [HttpPost]
        [MultipleButton(Name = "action", Argument = "GetIdsFlagsForClass")]
        public ViewResult GetIdsFlagsForClass(AssignIdsFlagsToClassViewModel vm)
        {
            _presentationLogic.SetAssignIdsFlagsToClassViewModel(HomePresenterTransformations.TransformAssignIdsFlagsToClassViewModel(vm));
            return View("AssignIdsFlagsToClass", _presentationLogic.GetAssignIdsFlagsToClassViewModel());
        }

        [HttpPost]
        [MultipleButton(Name = "action", Argument = "AssignIdsFlags")]
        public ActionResult AssignIdsFlags(AssignIdsFlagsToClassViewModel vm)
        {
            _idDManagementInteractor.AssignIdsFlagsToNewHireClass(
                HomeControllerTransformations.TransformAssignIdsFlagsToClassViewModelToRequest(vm));
            TempData[Alerts.SUCCESS] = "Ids and Flags successfully assigned to Class";
            return RedirectToAction("AssignIdsFlagsToClass", "Home");
        }

Open in new window


Here is my Razor View:
    <h2>@ViewBag.Title</h2>

    @using (Html.BeginForm(null, null, FormMethod.Post, new {id = "AssignIdsFlagsToNewHireClassForm"}))
    {
        <div class="col-md-12 small">
            <div class="single-field col-md-4">
                <label class="control-label" for="SelectedClass">@Html.DisplayNameFor(model => model.SelectedClass) : </label>
                @Html.DropDownListFor(model => model.SelectedClass, Model.NewHireClasses)
                <span class="alert-error">@Html.ValidationMessageFor(model => model.SelectedClass)</span>
            </div>
            <div class="single-field col-md-4">
                <input type="submit" id="SelectBtn" value="Select" name="action:GetIdsFlagsForClass" class="btn btn-success col-md-4"/>
            </div>
            @if (Model.DisplayAssignButton)
            {
                <div class="single-field col-md-4">
                    <input type="submit" id="AssignBtn" value="Assign Ids/Flags" name="action:AssignIdsFlags" class="btn btn-success col-md-4" />
                </div>
            }
            <div style="clear: both"></div>
        </div>
        <div class="col-md-12">
            <h3>IDs</h3>
            @Html.EditorFor(model => model.Ids)
        </div>
        <div class="col-md-12">
            <h3>Flags</h3>
            @Html.EditorFor(model => model.Flags)
        </div>
    }

Open in new window


Here is the Editor Template that is used by the model.Ids and model.Flags:
@model IDTPIApp.Models.DataStructures.FlagIdType
<div class="col-md-3 small">
    @Html.EditorFor(model => Model.IsSelected)@Html.HiddenFor(model => Model.TypeId)&nbsp; @Html.DisplayFor(model => model.DisplayName)
</div>

Open in new window


When the user clicks on Assign Ids/Flags the screen is redisplayed without any IDs or Flags shown (see the 1st screen in the attached file).  In order to get this to work I had to redirect to the AssignIdsFlagsToClass() method rather than resetting the model and returning the View.

Does anyone know why the View is behaving this way?
RazorViews.jpg
0
Comment
Question by:dyarosh
2 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 2000 total points
ID: 41805184
The problem is the HtmlHelper is showing the ModelState value, which has the posted data.  you can try clearning the model state (ModelState.Clear();) but a better appraoch would be to have a controller/view which takes the selected value as parameter and renders the data.
0
 

Author Closing Comment

by:dyarosh
ID: 41805199
Thanks.  That is what I ended up doing but wasn't sure why I had to.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question