Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default value for TextBoxFor in ASP.NET MVC

In ASP.NET MVC, I wrote below code to give the textbox a initial value:

@Html.TextBoxFor(p => p.WEIGHT, new { tabindex = "140", 
                                      @class = "mustInputText noime  w50", 
                                      maxlength = "8", @Value = "0", 
                                      rule = "InputOnlyNum" })

And the Html source is as follows:

<input Value="0" class="mustInputText noime  w50" id="WEIGHT" maxlength="8" 
    name="WEIGHT" rule="InputOnlyNum" tabindex="140" type="text" value="" />

I notices that there are two Value attributes in the "input" tag: Value="0" and value=""

How to make it only show one value attribute?

like image 391
roast_soul Avatar asked May 07 '13 07:05

roast_soul


People also ask

What is TextBoxFor in MVC?

TextBoxFor represents a single-line input control that allows end-users to enter text.

What is the difference between TextBoxFor and EditorFor in MVC?

TextBoxFor: It will render like text input html element corresponding to specified expression. In simple word it will always render like an input textbox irrespective datatype of the property which is getting bind with the control. EditorFor: This control is bit smart.

What is difference between TextBox and TextBoxFor?

IMO the main difference is that Textbox is not strongly typed. TextboxFor take a lambda as a parameter that tell the helper the with element of the model to use in a typed view. You can do the same things with both, but you should use typed views and TextboxFor when possible.


3 Answers

Use TextBox instead of TextBoxFor

@Html.TextBox("WEIGHT", Model.WEIGHT ?? "0", new {...})

or if WEIGHT is an empty string

@Html.TextBox("WEIGHT", Model.WEIGHT == "" ? "0" : Model.WEIGHT, new {...})
like image 178
rivarolle Avatar answered Oct 23 '22 23:10

rivarolle


It seems to be the default behavior. If you really want to avoid the double Value attributes, it's better to follow the cleaner way by setting the default value in the create method of the controller class. This follows the ideology of the MVC pattern.

//GET
public ActionResult CreateNewEntity()
{
    YourEntity newEntity= new YourEntity ();
    newEntity.WEIGHT= 0;

    return View(newEntity);
}

Then on your view, you won't need to use the value attribute anymore:

@Html.TextBoxFor(p => p.WEIGHT, new { tabindex = "140", 
                                      @class = "mustInputText noime  w50",     
                                      maxlength = "8", 
                                      rule = "InputOnlyNum" })

Your resulting html is:

<input class="mustInputText noime  w50" 
       id="WEIGHT" 
       maxlength="8" 
       name="WEIGHT" 
       rule="InputOnlyNum" 
       tabindex="140" 
       type="text" 
       value="0" />
like image 21
Bryan Hong Avatar answered Oct 23 '22 22:10

Bryan Hong


Well you've explicitly stated Value, not value.

Try:

 @Html.TextBoxFor(p => p.WEIGHT, new { tabindex = "140", @class = "mustInputText noime  w50", maxlength = "8", @value = "0", rule = "InputOnlyNum" })
like image 42
Darren Avatar answered Oct 23 '22 23:10

Darren