viki
viki

Reputation: 1188

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?

Upvotes: 30

Views: 134664

Answers (6)

Durgesh Singh
Durgesh Singh

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

Pankaj
Pankaj

Reputation: 61

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

Upvotes: 6

Bogdan Mates
Bogdan Mates

Reputation: 556

This works for me, in MVC5:

@Html.TextBoxFor(m => m.Name, new { @class = "form-control", id = "theID" , @Value="test" })

Upvotes: 4

Gaz Winter
Gaz Winter

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

viki
viki

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

dove
dove

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

Related Questions