Solved

ViewModel data appears to be caching in Razor View

Posted on 2016-09-14
2
59 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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