Solved

ViewModel data appears to be caching in Razor View

Posted on 2016-09-14
2
36 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
Comment Utility
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
Comment Utility
Thanks.  That is what I ended up doing but wasn't sure why I had to.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Total in textboxes 9 34
Converting C# Objects into JSON file 7 35
Hovering effect 9 28
C# code editing and collaboration 3 39
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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now