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

后端 未结 8 2284
谎友^
谎友^ 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 06:40

    What about "" instead of null.

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

    Also why isnt Loan.InterestRate nullable?

    <%: Html.TextBox("Loan.InterestRate", Model.Loan.InterestRate ?? "") %>
    
    0 讨论(0)
  • 2020-12-08 06:45

    Another one could be to check the value

    @Html.TextBoxFor
    (m => m.CertificateID, new { @Value = (Model.CertificateID > 0 ?Model.CertificateID.ToString() :string.Empty) })
    
    0 讨论(0)
  • 2020-12-08 06:46

    Here is what you can do: (VB Code)

    This is the core:

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

    Here is if you use the HTML to render your textbox

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

    Here is if you use HTML helper to render you textbox

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

    IIf is a very handy one line feature - the way it works is this.

    IIf(Expression, TruePart, FalsePart)
    

    So basically you saying - if my Parameter = 0 - I want to output nothing, String.Empty, Null, "" - and for the false part - it is like - oh if my Parameter does not equal to 0 - then that must be something other than 0 - so I will output the my numeric value without worrying that the zero will be in the textbox.

    Or however you want to structure it.

    Hope that helped!

    0 讨论(0)
  • 2020-12-08 06:48

    An alternative solution is to use jQuery to replace zeros with empty string when the page loads. Choose any name to use as a class name for all input fields which should be populated with empty string instead of zero. For example non-zero-num.

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

    And add the following script on your page:

    <script>
        $(document).ready(function(){
            $(".non-zero-num").val($(this).val() == 0 ? '' : $(this).val());
        })
    </script>
    
    0 讨论(0)
  • 2020-12-08 06:59

    In your model add DisplayFormat attribute

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

    View

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

    This outputs empty instead of 0. More format examples here

    Update

    TextBoxFor does`s not works with format, but EditorFor does:

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

    Update 2

    It does work for TextBoxFor this way:

    @Html.TextBoxFor(model => model.InterestRate, "{0:#.#}")
    
    0 讨论(0)
  • 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<Int32>" %>
    <%
        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)
    
    0 讨论(0)
提交回复
热议问题