Set a default value with Html.TextBoxFor in MVC4 and MVC3

6311
Views
1
Answers

I need to set a default value in HTML.TextBoxfor like

Html.TextBoxFor(m => m.Name, new { value = "Default value" }) 

How can I do that? the default value is added to the textbox.

edit | flag
Mad Maddy
Asked on: Jan 27, 2012 at 3:11AM

1 Answers

104
5
104

This should work for MVC3 & MVC4

 @Html.TextBoxFor(m => m.Name, new { @Value = "Default Name" })

If you want it to be a hidden field

 @Html.TextBoxFor(m => m.Name, new { @Value = "Default Name",@type="hidden" }) 

Note:

Do remember to use @Value with uppercase 'V' Because value with lowercase 'v' is a keyword in c#

edit | flag
Gokul A
Answered on: Jan 27, 2012 at 3:16AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags