I have a text box created using
@Html.TextBoxFor(m => m.Model1.field1, new { @class = "login-input", @name="Name", @Value = "test" })
I want to change the default value of this textbox from “text” to a value stored in a model field. How do i set a model field as the the value attribute? Say the Name of the model to be called is Model2 and the attribute is field2. How do I set the value of the textbox to field2?
You must first write an Extension Method like this:
It will extract the Name of property when you write sth like this:
@Html.TextBoxFor(m => m.Model1.field1)then you can use it like this:
If you don’t want to call
m => m.Model1.field1again, you must declare your version ofTextBoxFormethod which is more complicate, but If you want I can provide you the details.This is a sample from my code base on Github: