views:

935

answers:

3

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?

+3  A: 

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

Fyodor Soikin
+2  A: 

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
+3  A: 

you can try this

<%= Html.TextBoxFor(x => x.Age, new { @Value = "0"}) %>
Tassadaque
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
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