c# - Html.TextboxFor default value for Integer/Decimal to be Empty instead of 0 -
i using default asp.net mvc 2 syntax construct textbox's integer or decimal asp.net mvc web app:
<%: html.textboxfor(model => model.loan.interestrate) %>
pretty straight forward, problem inherently of fact binding model objects decimal or int , non-nullable, print value 0 (0) on page load if model empty (such in add mode crud template) , 0 incorrect value , invalid page validation also.
how have textboxes have no starting value, empty textbox, understand 0 potential value, accept values greater 0 anyway, not problem me.
i tried casting nullable decimal , non-for helper (which not ideal), alas, still receiving default '0' value. ideas??
<%: html.textbox("loan.interestrate", model.loan.interestrate == 0 ? (decimal?)null : model.loan.interestrate) %>
thanks.
you can override default template putting custom template in /shared/editortemplates or controller in /controllername/editortemplates.
i use 1 int's. named 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)%>
Comments
Post a Comment