ASP.NET MVC中TextBoxFor的默认值 [英] Default value for TextBoxFor in ASP.NET MVC
本文介绍了ASP.NET MVC中TextBoxFor的默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在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" })
HTML来源如下:
<input Value="0" class="mustInputText noime w50" id="WEIGHT" maxlength="8"
name="WEIGHT" rule="InputOnlyNum" tabindex="140" type="text" value="" />
我注意到"input"标签中有两个Value属性:
Value="0"
和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?
推荐答案
使用TextBox代替TextBoxFor
Use TextBox instead of TextBoxFor
@Html.TextBox("WEIGHT", Model.WEIGHT ?? "0", new {...})
或者如果WEIGHT是一个空字符串
or if WEIGHT is an empty string
@Html.TextBox("WEIGHT", Model.WEIGHT == "" ? "0" : Model.WEIGHT, new {...})
这篇关于ASP.NET MVC中TextBoxFor的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文