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)