I have created an text-box using Razor and trying to set value
as follows.
@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", value= "3" })
I have tried appending value
with @
@Html.TextBoxFor(model=> model.Destination, new { id = "txtPlace", @value= "3" })
even though it renders html input
tag with empty value
<input id="txtPlace" name="Destination" type="text" value class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset ui-mini" >
What am doing wrong?
Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.
The TextBox for the Name value is created using Html. TextBoxFor function while the TextBox for the Mobile Number value is created using Html. TextBox helper function. The MaxLength of both the TextBoxes is set using the HTML MaxLength attribute using the HtmlAttributes parameter in Html.
TextBoxFor<TModel,TProperty>(HtmlHelper<TModel>, Expression<Func<TModel,TProperty>>, Object) Returns a text input element for each property in the object that is represented by the specified expression, using the specified HTML attributes.
IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.
The problem is that you are using a lower case v.
You need to set it to Value and it should fix your issue:
@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", Value= "3" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With