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

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

    
    

    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!

提交回复
热议问题