Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set value in @Html.TextBoxFor in Razor syntax?

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?

like image 654
viki Avatar asked Dec 21 '12 12:12

viki


People also ask

How do you give ID in Razor syntax?

Just add the id property to the html-attributes. That will override the default id generated by the editorfor-helper-methode.

How do I set the MaxLength for HTML TextBoxFor in MVC?

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.

What is HTML TextBoxFor?

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.

What is the difference between HTML TextBox vs HTML TextBoxFor?

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.


1 Answers

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" }) 
like image 199
Gaz Winter Avatar answered Sep 28 '22 15:09

Gaz Winter