215

Simple question, if you use the Html Helper from ASP.NET MVC Framework 1 it is easy to set a default value on a textbox because there is an overload Html.TextBox(string name, object value). When I tried using the Html.TextBoxFor method, my first guess was to try the following which did not work:

<%: Html.TextBoxFor(x => x.Age, new { value = "0"}) %>

Should I just stick with Html.TextBox(string, object) for now?

dcompiled
  • 4,762
  • 6
  • 33
  • 36

13 Answers13

388

Try this:

<%= Html.TextBoxFor(x => x.Age, new { @Value = "0"}) %>

note that @Value has a capital V

Matthew
  • 1,630
  • 1
  • 14
  • 19
Tassadaque
  • 8,129
  • 13
  • 57
  • 89
  • 55
    Curious to know why capital 'V' works and lowercase 'v' does not? Also, this solution overrides the model value for Age, even if one is present. – Derek Hunziker Sep 01 '10 at 23:46
  • 78
    value with small v is keyword for C# http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx. So i think thats why it doesn't work. – Tassadaque Sep 02 '10 at 04:36
  • 4
    Upvoted the comment because one upvote for an answer this obscure and insightful seemed... inadequate. – ehdv Mar 29 '11 at 19:56
  • 9
    It is still curious that @value doesn't work. readonly is also a keyword for C# but @readonly works without any problems. – Diego May 30 '11 at 12:54
  • 16
    The html source in browser is when using @Value. There're both "Value" with "0" and "value" with "" in the input tag. – Ricky Dec 19 '11 at 09:22
  • What would be the same for Vb.net? – mrN Aug 23 '12 at 18:52
  • http://stackoverflow.com/questions/14142511/textboxfor-value-uppercase-instead-value – Diego Jan 03 '13 at 16:36
  • in this answer is correct. value = "0" this will not work. Value = "0" this will work. Because V is capital letter. need to include at also. – Thulasiram Feb 22 '13 at 12:09
  • 2
    `value` is a contextual keyword in C#, and only as special meaning in property getters and setters. My guess is that the MVC code is hard coded to look for "Value" because MS intends you to always use PascalCase property names since that's their typical convention and to avoid conflicts with non-contextual keywords such as `class`. It all gets rendered in the HTML as lowercase anyway. [more](http://stackoverflow.com/a/15271804/145173) – Edward Brey Mar 07 '13 at 12:54
  • How to achieve same with "TextAreaFor" ? default values plus row and col – Anil Purswani Oct 07 '14 at 05:21
  • 1
    I'd like to add that this may be the solution for MVC 1, but for any newer version this is unncessary. You shouldn't need to specify @Value, the HtmlHelpers should automatically show a value in the textbox if the model property already has a value. I'm not sure on which version of MVC this was added, but it's something to keep in mind. – Paul Nov 14 '16 at 22:31
  • 1
    **NEVER** set the `value` attribute when using `HtmlHelper` methods. You set the value of the property in the GET method before you pass the model to the view. –  Dec 05 '17 at 11:16
63

This should work for MVC3 & MVC4

 @Html.TextBoxFor(m => m.Age, new { @Value = "12" }) 

If you want it to be a hidden field

 @Html.TextBoxFor(m => m.Age, new { @Value = "12",@type="hidden" }) 
Gokul
  • 1,361
  • 3
  • 19
  • 31
31

It turns out that if you don't specify the Model to the View method within your controller, it doesn't create a object for you with the default values.

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Create()
{
  // Loads default values
  Instructor i = new Instructor();
  return View("Create", i);
}

[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Create()
{
  // Does not load default values from instructor
  return View("Create");
}
dcompiled
  • 4,762
  • 6
  • 33
  • 36
  • More likely it is MVC bug or feature, since Html.PasswordFor(m => m.Password, new { class = "form-control", value ="default" is rendered into almost the same html, and the "default" go to value being displayed successfully. For example, value=Model.Email works, but value=ViewBag.Email does work. So this accepted answer is better and cleaner, not so hacking. – ZZZ Oct 27 '14 at 13:01
  • What is `Instructor`? – Csaba Toth May 14 '16 at 18:32
22

The default value will be the value of your Model.Age property. That's kind of the whole point.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
  • 3
    Seems it is bad idea when model manages default values. What should be done when different default value needs for same collection of model items? – RredCat Nov 03 '10 at 17:52
  • @RredCat Agreed, this muddies the separation of the UI layer – Evan M May 03 '12 at 19:28
21

You can simply do :

<%= Html.TextBoxFor(x => x.Age, new { @Value = "0"}) %>

or better, this will switch to default value '0' if the model is null, for example if you have the same view for both editing and creating :

@Html.TextBoxFor(x => x.Age, new { @Value = (Model==null) ? "0" : Model.Age.ToString() })
Chtioui Malek
  • 11,197
  • 1
  • 72
  • 69
13

This work for me

@Html.TextBoxFor(model => model.Age, htmlAttributes: new { @Value = "" })
  • +1 for the named parameter that is especially helpful in copy/pastable code and one that uses a method with many overloads – Ekus Apr 10 '19 at 20:51
11

value="0" will set defualt value for @Html.TextBoxfor

its case sensitive "v" should be capital

Below is working example:

@Html.TextBoxFor(m => m.Nights, 
    new { @min = "1", @max = "10", @type = "number", @id = "Nights", @name = "Nights", Value = "1" })
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
asif jan
  • 129
  • 1
  • 4
9

Here's how I solved it. This works if you also use this for editing.

@Html.TextBoxFor(m => m.Age, new { Value = Model.Age.ToString() ?? "0" })
user3738893
  • 425
  • 1
  • 6
  • 18
8

Using @Value is a hack, because it outputs two attributes, e.g.:

<input type="..." Value="foo" value=""/>

You should do this instead:

@Html.TextBox(Html.NameFor(p => p.FirstName).ToString(), "foo")
Max Toro
  • 28,282
  • 11
  • 76
  • 114
8

this worked for me , in this way we setting the default value to empty string

@Html.TextBoxFor(m => m.Id, new { @Value = "" })
eric_eri
  • 699
  • 11
  • 19
  • 2
    While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono Jan 30 '16 at 10:33
  • @Bono added some comments – eric_eri Feb 01 '16 at 16:45
5

If you have a partial page form for both editing and adding, then the trick I use to default value to 0 is to do the following:

@Html.TextBox("Age", Model.Age ?? 0)

That way it will be 0 if unset or the actual age if it exists.

andr
  • 15,970
  • 10
  • 45
  • 59
2

For .net core 5 setting value in htmlAttributes seems doesnt work. But you can use workaround:

var ageTextBox = (TagBuilder) Html.TextBoxFor(x => x.Age);
ageTextBox.Attributes.Remove("value");
ageTextBox.Attributes.Add("value", "value you want to set");
alanextar
  • 1,094
  • 13
  • 16
0

Try this also, that is remove new { } and replace it with string.

<%: Html.TextBoxFor(x => x.Age,"0") %>
TheTechGuy
  • 16,560
  • 16
  • 115
  • 136