HtmlHelper.TextBox uses the model value, even if an explicit value is specified

In the view, I use this HtmlHelper.TextBox overload :

 public static MvcHtmlString TextBox( this HtmlHelper htmlHelper, string name, Object value ) 

The documentation states:

value

Type: System.Object

The value of the text input element. If this value is null , the value of the item is retrieved from the ViewDataDictionary . If there is no value, the value is retrieved from the ModelStateDictionary .

I provide value when I call this overload, and this value is non-zero. However, the value for the text ModelStateDictionary is retrieved from ModelStateDictionary whenever it is present there.

To force the text field to use the value provided by the built-in, I need to first reset the model in the controller (or remove the key with the name of the text field from the key collection).

The same applies to other controls displayed by the HtmlHelper .

Where is my understanding wrong? Or is this a mistake in the documentation?

+6
source share
3 answers

ModelState always takes precedence over all input helpers, not just TextBox . This allows you to specify a default value in the view even after the POST displays the published value (taken from ModelState) without having to manually pass the published value to the view. To use a different value, you must remove the entry from ModelState.

+2
source

This is how the model binder works, it takes values ​​from modelstate, possibly to save values ​​from failure, unsuccessful reverse or fault tolerance. Have you tried using the Html attributes to set the value

 Html.TextBox("Id", new { Value = Model.Id}) 

Si

+1
source

Perhaps this will help.

Using viewmodel with the property below and ViewData ["textboxvalue"]

 public string Id { get; set; } 

If you provide an object that can be bound to the value parameter, the html helper will always use its value.

- Text field attached to the view model - this shows the model identifier

 @Html.TextBox("Id", Model.Id) 

-Text box with the provided value from the view data - they display the value of the view data

 @Html.TextBox("Id", ViewData["textboxvalue"]) 

Even if the specified name does not match the property name, the value will still match the model

- Text field attached to the view model - this shows the model identifier

 @Html.TextBox("Id2", Model.Id) 

-Text box with the provided value from the view data - they display the value of the view data

 @Html.TextBox("Id2", ViewData["textboxvalue"]) 

If you provide your own

-Text box with a hard-coded value provided - this shows a line represented as

 @Html.TextBox("Id", "my hardcoded value in view") 

The documentation states - if you do not fill in the "value" parameter, as shown below:

 @Html.TextBox("Id", null) 

Then, the mvc framework will automatically try to search for a model that is tied to the view and data dictionary.

If you have this case,

- Holds the "Id" property in the view model. Text field value set to model value

 @Html.TextBox("Id", null) 

But if the value of "name" is not a property / key in the model / dictionaries, then the framework could not find any value, so the value of the text field will be an empty string

-Text box with a name that is not property in the model view or data view

 @Html.TextBox("Id2", null) 
-1
source

Source: https://habr.com/ru/post/924596/


All Articles