TextBoxFor displaying initial value, not the value updated from code

Adrien Tancrez

I have an MVC application that displays a value. This is the controller:

public ActionResult Index(DataSites DataSiteList)
{
    if (DataSiteList.Latitude != null)
    {
        DataSites test = new DataSites();
        test.Latitude = "LATITUDE";

        return View(test);
    }
    return View(DataSiteList);
}
public ActionResult SomeInformation()
{
    DataSites test1 = new DataSites();
    test1.Latitude = "LATITUDE2";

     return RedirectToAction("Index", test1);
}

The View:

@model miniproj2.Models.DataSites

<p>
    @Html.TextBoxFor(x => x.Latitude)
</p>

And the Model:

public class DataSites
{
    public string Latitude { get; set; }
}

When I go to /Home/SomeInformation, the DataSites' Latitude property is set to "LATITUDE2". Then redirects to the Index() action in the controler, sets the property to "LATITUDE" and returns the view.

When it shows the view, it displays the value "LATITUDE2" as set in the redirect. Shouldn't "LATITUDE" be displayed?

user3559349

Your problem is (step by step)

  1. Your SomeInformation() method sets the value of test1.Latitude to "LATITUDE2".
  2. You then pass that model to your Index() method using the overload of RedirectToAction that accepts an object. Internally this uses reflection to build a RouteValueDictionary based on the properties of your model (in this case its simply latitude="LATITUDE2").
  3. When you hit the Index method the model is bound by the DefaultModelBinder and now the value of DataSiteList.Latitude is "LATITUDE2" (which is why you enter the if block)
  4. In the process of binding, the DefaultModelBinder sets the ModelStatevalue of Latitude to "LATITUDE2". Any attempts to set the value of Latitude are now ignored because the view uses ModelState value to render the control.

It not clear what your trying to do here. You can make it work as you expect by adding ModelState.Clear(); as the first line of your Index() method. This clears all existing ModelState values an you can now set the value to "LATITUDE".

But your if block makes no sense. Perhaps you were just doing some kind of test, but you may as well remove the parameter from the Index() method and just initialize a new instance of DataSites in the method.

Edit

To give a bit more information as to why updating a model property has no affect once ModelState has been set.

Imagine you have a form to collect user information where the model contains int Age. The user is asked to enter their age and someone enters "I'm five next week!". Of course this wont bind to an int so the DefaultModelBinder adds the value (the attemptedValue) and adds a ModelStateError.

When the view is returned it will typically display an error message such as "The field Age must be a number". If the html helper rendering the control used the model value, then it would display "0" (the default value for int). It would be somewhat confusing for the user to see "0" in the textbox and next it a message saying it must be a number (What! but zero is a number and what the heck happened to what I entered?). So instead, the helper uses the value from ModelState and now the users sees "I'm five next week!" and an associated error message that makes sense for the value.

So even though you thoughts were that "its not logical", there is actually some logic to this behavior.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

DataGridComboBoxColumn initial value not displaying

Displaying updated value from Angular service

Primefaces selectOneMenu not displaying initial value

Displaying initial value in ComboBox WPF

localStorage getting updated with initial state value on refresh

String interpolation in Angular 2 not displaying updated value

Why query not updated value but get initial value in Function with ExtReact

More efficient code for displaying EQ images from value

Ajax return value displaying code

Start UIStepper from initial value

How to retrive value from @Html.TextBoxFor in a external javascript file

Second Form Action, how to get value from Html.TextBoxFor

Value From DialogFragment to fragment is not displaying

Retrieving and displaying value from firebase

While loading the data from csv file into oracle db using python code True value is displaying as 1 and false value is displaying as 0

get value from updated row

Not getting updated value from store

Reset state to initial value instead of most updated recent one

React Hooks, updated value of setState shows initial values in another component

TextBoxFor: INumber type but with default value = ""

Required value in Html.TextBoxFor

Following code displaying NAN and INF value on screen

Can a code run if initial array value is less then input value?

How to code a for or while loop wich prints list's values from a initial index value to a end one?

Add column with initial value from another table

Check if Reactive form value is changed from initial

SwiftUI Setting Initial Picker Value From CoreData

Pandas pct change from initial value

Fold with initial value from localStorage in CycleJS