Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is the code from the MVC Source for the textbox:</p> <pre><code> string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string)); tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : valueParameter**), isExplicitValue); break; </code></pre> <p>And the Code for GetModelStateValue()</p> <pre><code> internal object GetModelStateValue(string key, Type destinationType) { ModelState modelState; if (ViewData.ModelState.TryGetValue(key, out modelState)) { if (modelState.Value != null) { return modelState.Value.ConvertTo(destinationType, null /* culture */); } } return null; } </code></pre> <p>So what happens is the Html "Helper" looks for the text box value, by matching the name, in your ViewData.ModalState, if its in the ModelState dictionary, it <strong>completely ignores</strong> the value you provided.</p> <p>So all that if (value > 0) { ValueA = 0; } doesn't matter because its going to use the posted values in ModelState if the names match.</p> <p>The way I've fixed this is to blow away the ModalState before the view renders for certain values that I want to mess with in my view models. This is some code I've used:</p> <pre><code> public static void SanitizeWildcards( Controller controller, params string[] filterStrings ) { foreach( var filterString in filterStrings ) { var modelState = controller.ModelState; ModelState modelStateValue; if( modelState.TryGetValue(filterString,out controller.ModelState.SetModelValue(filterString, new ValueProviderResult("","", null)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload