How to set a default value with Html.TextBoxFor?

C#asp.net Mvc-2Html Helper

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

Try this:

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

note that @Value has a capital V

Solution 2 - C#

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" }) 

Solution 3 - C#

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");
}

Solution 4 - C#

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

Solution 5 - C#

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() })

Solution 6 - C#

This work for me

@Html.TextBoxFor(model => model.Age, htmlAttributes: new { @Value = "" })

Solution 7 - C#

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" })

Solution 8 - C#

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" })

Solution 9 - C#

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")

Solution 10 - C#

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

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

Solution 11 - C#

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.

Solution 12 - C#

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

<%: Html.TextBoxFor(x => x.Age,"0") %>

Solution 13 - C#

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");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondcompiledView Question on Stackoverflow
Solution 1 - C#TassadaqueView Answer on Stackoverflow
Solution 2 - C#GokulView Answer on Stackoverflow
Solution 3 - C#dcompiledView Answer on Stackoverflow
Solution 4 - C#Fyodor SoikinView Answer on Stackoverflow
Solution 5 - C#Chtiwi MalekView Answer on Stackoverflow
Solution 6 - C#Andrew AndriichukView Answer on Stackoverflow
Solution 7 - C#asif janView Answer on Stackoverflow
Solution 8 - C#user3738893View Answer on Stackoverflow
Solution 9 - C#Max ToroView Answer on Stackoverflow
Solution 10 - C#eric_eriView Answer on Stackoverflow
Solution 11 - C#Harel SeligmannView Answer on Stackoverflow
Solution 12 - C#Hammad KhanView Answer on Stackoverflow
Solution 13 - C#alanextarView Answer on Stackoverflow