Reputation: 1188
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?
Upvotes: 30
Views: 134664
Reputation: 29
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" >
Upvotes: 0
Reputation: 61
I tried replacing value
with Value
and it worked out. It has set the value
in input
tag now.
Upvotes: 6
Reputation: 556
This works for me, in MVC5:
@Html.TextBoxFor(m => m.Name, new { @class = "form-control", id = "theID" , @Value="test" })
Upvotes: 4
Reputation: 2989
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" })
Upvotes: 76
Reputation: 1188
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" })
Upvotes: 10
Reputation: 20674
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.
Upvotes: 8