Solved

ViewModel data appears to be caching in Razor View

Posted on 2016-09-14
2
68 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 29

Accepted Solution

by:
Gautham Janardhan earned 500 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

707 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