Html.TextboxFor整数/十进制的默认值为空而不是0

我使用默认的asp.net MVC 2语法来构build我的asp.net MVC Web应用程序的整数或小数的TextBox的:

<%: Html.TextBoxFor(model => model.Loan.InterestRate) %> 

相当直接,但问题是事实上,我的绑定模型对象是十进制或整数和不可为空,如果我的模型是空的,他们在页面加载时将其值打印为零(0) CRUD模板),零是一个不正确的值,也是我的页面validation无效。

我怎么能有文本框没有起始值,只是一个空的文本框,我知道零是一个潜在的价值,但我只接受大于零的值,所以这对我来说不是一个问题。

我什至试图铸造为一个可为空的十进制,也是一个非帮助(这是不理想的),但唉,我仍然收到默认的“0”值。 有任何想法吗??

 <%: Html.TextBox("Loan.InterestRate", Model.Loan.InterestRate == 0 ? (decimal?)null : Model.Loan.InterestRate) %> 

您可以通过在/ Shared / EditorTemplates中或通过/ ControllerName / EditorTemplates中的控制器放置自定义模板来覆盖默认模板。

我用这一个诠释的。 它的名字是Int32.ascx:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <%@ Import Namespace="System.Web.Mvc.Html" %> <% string displayText = string.Empty; if (Model != null) { displayText = Model.ToString(); } %> <%= Html.TextBox("", displayText)%> 

我使用这个解决scheme:

  @Html.TextBoxFor(model => model.Year, new { Value = "" }) 

请参阅: https : //stackoverflow.com/a/6186576/297487

在你的模型中添加DisplayFormat属性

 [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:#.#}")] decimal InterestRate { get; set; } 

视图

 @Html.TextBoxFor(model => model.InterestRate) 

这将输出空而不是0.更多的格式示例在这里

更新

TextBoxFor不适用于格式,但EditorFor:

 @Html.EditorFor(model => model.InterestRate) 

更新2

它为TextBox工作这样:

 @Html.TextBoxFor(model => model.InterestRate, "{0:#.#}") 

另一个可能是检查价值

 @Html.TextBoxFor (m => m.CertificateID, new { @Value = (Model.CertificateID > 0 ?Model.CertificateID.ToString() :string.Empty) }) 

怎么样“”而不是null。

  <%: Html.TextBox("Loan.InterestRate", Model.Loan.InterestRate == 0 ? "" : Model.Loan.InterestRate) %> 

另外为什么不是Loan.InterrestRate为空?

 <%: Html.TextBox("Loan.InterestRate", Model.Loan.InterestRate ?? "") %> 

另一种解决scheme是使用jQuery在页面加载时用空stringreplace零。 select任何名称用作所有input字段的类名,这些input字段应该用空string而不是零填充。 例如非零数字。

 @Html.TextBoxFor(model => model.InterestRate, new { @class = "non-zero-num" }) 

并在您的页面上添加以下脚本:

 <script> $(document).ready(function(){ $(".non-zero-num").val($(this).val() == 0 ? '' : $(this).val()); }) </script> 

我认为@jfar的答案指向正确的方向,但Int32.ascx应该是这样的

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Int32>" %> <% string displayText; if (Model == 0) { displayText = ""; } else { displayText = Model.ToString(); } %> <%= Html.TextBox("", displayText)%> 

或使用Razor(/Shared/EditorTemplates/Int32.cshtml)更新答案

 @model Int32 @{ string text = Model == 0 ? "" : Model.ToString(); } @Html.TextBox("", text) 

这里是你可以做的:(VB代码)

这是核心:

@IIf(Model.YourNumericValue = 0, "", Model.YourNumericValue)

这里是如果您使用HTML来呈现您的文本框

 <input type="text" name="sYourTextboxName" value="@IIf(Model.YourNumericValue= 0, "", Model.YourNumericValue))"> 

这里是如果您使用HTML助手来呈现您的文本框

  @Html.TextBox("sYourTextboxName", IIf(Model.YourNumericValue= 0, "", Model.YourNumericValue)) 

IIf是一个非常方便的一行function – 它的工作方式是这样的。

 IIf(Expression, TruePart, FalsePart) 

所以基本上你说 – 如果我的参数= 0 – 我不想输出什么,String.Empty,Null,“” – 对于错误的部分 – 就像 – 如果我的参数不等于0 – 那么必须不是0的东西 – 所以我会输出我的数值,而不用担心零将在文本框中。

或者你想要构build它。

希望有所帮助!