DisplayFormat for TextBoxFor in MVC

后端 未结 5 510
孤独总比滥情好
孤独总比滥情好 2020-12-09 02:56

I need to round off 4 digit decimal to 2 digits and show in MVC 3 UI

Something like this 58.8964 to 58.90

Tried following this How should I

5条回答
  •  情歌与酒
    2020-12-09 03:21

    I solve that issue in this way:

    @Html.TextBoxFor(model => model.dtArrivalDate, ModelMetadata.FromLambdaExpression(model => model.dtArrivalDate, ViewData).EditFormatString)
    

    or create next extension:

    public static class HtmlExtensions
    {
        public static MvcHtmlString TextBoxWithFormatFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes)
        {
            return htmlHelper.TextBoxFor(expression, ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).EditFormatString, htmlAttributes);
        }
    }
    

    But you need to set ApplyFormatInEditMode=true in DisplayFormatAttribute on your field.

提交回复
热议问题