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
Solved

ViewModel data appears to be caching in Razor View

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

790 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