30

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?

dove
  • 20,469
  • 14
  • 82
  • 108
viki
  • 1,178
  • 1
  • 17
  • 22
  • possible duplicate of [How to set a default value with Html.TextBoxFor?](http://stackoverflow.com/questions/3034986/how-to-set-a-default-value-with-html-textboxfor) – Keith Pinson Jul 31 '13 at 14:05

6 Answers6

76

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" })
Gaz Winter
  • 2,924
  • 2
  • 25
  • 47
10

I tried replacing value with Value and it worked out. It has set the value in input tag now.

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", Value= "3" })
viki
  • 1,178
  • 1
  • 17
  • 22
  • 3
    Although it will work, I think a more neat way is to initialize the Destination property of your model in the Controller. – Romias Dec 21 '12 at 13:04
  • @Romias it depends on the use case. For example, I have a model that is used for both an edit dialog and a search dialog. In the edit dialog, it is OK. Default values are initialized properly and even `int` properties appear initialized with `0`. However, for the search dialog, I need all the fields to be initially empty for obvious reasons. That is why the use of setting the value in the razor view makes sense. I don't know the real use case of the original question, though. – jstuardo May 20 '20 at 15:57
8

It is going to write the value of your property model.Destination

This is by design. You'll want to populate your Destination property with the value you want in your controller before returning your view.

dove
  • 20,469
  • 14
  • 82
  • 108
  • It doesn't always. If your route object contains a Destination parameter then MVC will use this in preference to the Destination property in your Model. http://stackoverflow.com/questions/7251241/textboxfor-showing-wrong-value/7251446 – Giles Roberts Feb 06 '15 at 08:15
6

I tried replacing value with Value and it worked out. It has set the value in input tag now.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
Pankaj
  • 61
  • 1
  • 1
4

This works for me, in MVC5:

@Html.TextBoxFor(m => m.Name, new { @class = "form-control", id = "theID" , @Value="test" })
Bogdan Mates
  • 520
  • 4
  • 8
0

Tries with following it will definitely work:

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", Value= "3" })

@Html.TextBoxFor(model => model.Destination, new { id = "txtPlace", @Value= "3" })

<input id="txtPlace" name="Destination" type="text" value="3" class="ui-input-text ui-body-c ui-corner-all ui-shadow-inset ui-mini" >