Html.TextboxFor default value for Integer/Decimal to be Empty instead of 0

后端 未结 8 2290
谎友^
谎友^ 2020-12-08 06:36

I am using the default asp.net MVC 2 syntax to construct TextBox\'s which are integer or decimal for my asp.net MVC web app:

<%: Html.TextBoxFor(model =&g         


        
8条回答
  •  春和景丽
    2020-12-08 07:02

    I think @jfar's answer points in the right direction but the Int32.ascx should be this

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

    or updated answer using Razor (/Shared/EditorTemplates/Int32.cshtml)

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

提交回复
热议问题