DisplayFormat为TextBoxFor在MVC [英] DisplayFormat for TextBoxFor in MVC

查看:335
本文介绍了DisplayFormat为TextBoxFor在MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要4位十进制四舍五入到2位数,展现在MVC 3 UI

事情是这样的58.8964至58.90

试过下面这个<一个href=\"http://stackoverflow.com/questions/5080451/how-should-i-use-editorfor-in-mvc-for-a-currency-money-type\">How我应该使用MVC EditorFor()的货币/货币类型?但不工作。


  

由于我使用我TextBoxFor =>我删除ApplyFormatInEditMode在这里。甚至
  我试着用ApplyFormatInEditMode,但没有任何工程。仍呈现
  我58.8964。


MyModelClass

  [DisplayFormat(DataFormatString ={0:F2})]
 公共小数?总金额{搞定;组; } @ Html.TextBoxFor(M = GT; m.TotalAmount)

我怎样才能做到这轮关闭?

我不能使用EditorFor(M => m.TotalAmount)在这里,因为我需要通过一些htmlAttributes

编辑:

与MVC源$ C ​​$ C调试后,他们在内部使用

 字符串valueParameter = Convert.ToString(值,CultureInfo.CurrentCulture);

在MvcHtmlString InputHelper()InputExtension.cs的方法,该方法的目标值为的参数和转换。他们没有使用任何有显示格式。我们怎么能解决?

我设法用这种方式来解决。因为我有一个自定义的帮手,我可以能够与低于code管理

 如果(!string.IsNullOrEmpty(modelMetaData.DisplayFormatString))
   {
     字符串的formatString = modelMetaData.DisplayFormatString;
     字符串FormattedValue的=的String.Format(CultureInfo.CurrentCulture,formatString中,modelMetaData.Model);
     字符串名称=前pressionHelper.GetEx pressionText(如pression);
     字符串全名= htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(名);
       返回htmlHelper.TextBox(全名,FormattedValue的,htmlAttributes);
   }
   其他
   {
       返回htmlHelper.TextBoxFor(如pression,htmlAttributes);
   }


解决方案

您应该使用 Html.EditorFor 而不是 Html.TextBoxFor 如果要考虑到自定义格式:

  @ Html.EditorFor(M = GT; m.TotalAmount)

另外,还要确保您已设置 ApplyFormatInEditMode 为true:

  [DisplayFormat(DataFormatString ={0:F2},ApplyFormatInEditMode = TRUE)]
公共小数?总金额{搞定;组; }

该DisplayFormat属性旨在与模板佣工只用于诸如 EditorFor DisplayFor 。这是推荐的方法,而不是使用 TextBoxFor

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 use EditorFor() in MVC for a currency/money type? but not working.

As i am using TextBoxFor=> i removed ApplyFormatInEditMode here. Even i tried with ApplyFormatInEditMode , but nothing works. Still showing me 58.8964.

MyModelClass

 [DisplayFormat(DataFormatString = "{0:F2}")]
 public decimal? TotalAmount { get; set; }

 @Html.TextBoxFor(m=>m.TotalAmount)

How can i achieve this round off?

I can't use EditorFor(m=>m.TotalAmount) here, as i need to pass some htmlAttributes

Edit:

After debugging with MVC source code, they internally use

 string valueParameter = Convert.ToString(value, CultureInfo.CurrentCulture);

in MvcHtmlString InputHelper() method of InputExtension.cs that takes object value as parameter and converting. They are not using any display format there. How could we fix?

I managed to fix in this way. As i have a custom helper, i can able to manage with the below code

 if (!string.IsNullOrEmpty(modelMetaData.DisplayFormatString))
   {
     string formatString = modelMetaData.DisplayFormatString;
     string formattedValue = String.Format(CultureInfo.CurrentCulture, formatString, modelMetaData.Model);
     string name = ExpressionHelper.GetExpressionText(expression);
     string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
       return htmlHelper.TextBox(fullName, formattedValue, htmlAttributes);
   }
   else
   {
       return htmlHelper.TextBoxFor(expression, htmlAttributes);
   }

解决方案

You should use Html.EditorFor instead of Html.TextBoxFor if you want the custom format to be taken into account:

@Html.EditorFor(m => m.TotalAmount)

Also make sure that you have set ApplyFormatInEditMode to true:

[DisplayFormat(DataFormatString = "{0:F2}", ApplyFormatInEditMode = true)]
public decimal? TotalAmount { get; set; }

The DisplayFormat attribute is intended to be used only with templated helpers such as EditorFor and DisplayFor. This is the recommended approach instead of using TextBoxFor.

这篇关于DisplayFormat为TextBoxFor在MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆