1

I have a textbox that I am trimming the value of server side in the viewModel's setter and verifying that the trimmed value is present:

    <Required()>
    Public Property Name As String
        Get
            Return Me.mName
        End Get
        Set(value As String)
            Me.mName = value.Trim()
        End Set
    End Property

In the controller, I check to see if the ModelState is invalid, and if it is, I show the view again:

    If (Not ModelState.IsValid) Then
        Return View("Locations", viewModel)
    End If

In situations where the trimmed value is blank, I re-display the form to the user. At this point, the non-trimmed values are put into the textbox. I would much prefer the trimmed value.

I was looking through the MVC sourcecode, and I found that at one point in the InputHelper function (used by Html.Textbox(), located in InputExtensions.cs) the code was checking the ModelState for the value, and if it existed, replacing the controller-supplied value.

    string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string));
    tagBuilder.MergeAttribute("value", attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : valueParameter), isExplicitValue);

I realize that I could fix this by clearing the ModelState in the controller, but I'm just wondering why the MVC Developers chose to have the values in the ModelState supersede the view model values.

3
  • It's a design decision that only the ASP.NET MVC developers/architects could answer (as they were the ones design the framework you are asking about). SO is not the right place to ask those kind of questions. Please address yourself directly to the editors of the software you are using (which in your case would be Microsoft). Commented Oct 3, 2011 at 21:49
  • 2
    I wasn't asking about the design of the code. I was asking about a specific feature that was purposely implemented by the MVC Team. I was wondering about the specific feature of MVC that this corresponds to. Commented Oct 3, 2011 at 21:59
  • you are asking why Html helpers first look at ModelState when binding their values and then at the view model value and the answer to this question is (as you've already found by looking at the source code): by design. The reason why the designers designed to design the framework in such a way could be best answered by the designers of the framework themselves. Commented Oct 3, 2011 at 22:03

1 Answer 1

3

This was designed this way because the assumption is if you are not redirecting to another view after a post .. Hence the post-redirect-get pattern, then there was ideally an error and the current posted values would be shown back to the user for them to fix hence why they are pulled from modelstate. You can ModelState.Clear to work with this but keep that design in mind.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.