DisplayFormat没有得到应用的十进制值 [英] DisplayFormat not getting applied on decimal value

查看:222
本文介绍了DisplayFormat没有得到应用的十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要使用EditorFor模板来渲染模型属性,我想申请使用DisplayFormat属性格式化。但是,它不工作的 - 这是完全被忽略了。

下面是我的模板:

  @model System.Decimal?
@ Html.TextBoxFor(M =>米)

下面是我的模型:

  [DisplayFormat(ApplyFormatInEditMode = TRUE,DataFormatString ={0:0.00})]
公共小数?零售{搞定;组; }

下面是我的看法:

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

但它呈现了如下的值一个文本框:


  

189.9900


这似乎pretty直线前进,但它不工作,我不知道为什么。

更新:
只是踢,我用DisplayFor模板试了一下,和它的工作:

  @ Html.DisplayFor(M = GT; m.Retail)

那么,为什么会在DisplayFor模板的工作,但不是EditorFor模板,当我设置了 ApplyFormatInEditMode 来真的吗?

更新2:
没关系,这工作的原因是因为我的十进制显示模板是硬coded到这种方式进行格式化。所以,我的显示模板也不起作用。


解决方案

达林季米特洛夫发布<一个href=\"http://stackoverflow.com/questions/5428289/mvc3-decimal-truncated-to-2-decimal-places-on-edit\">this回答,我能得到它的工作使用他的解决方案:

  @ Html.TextBox(,ViewData.TemplateInfo.FormattedModelValue)

有点粗,海事组织,这不起作用瓦特/ TextBoxFor ,但至少它的工作原理。

I have a model property I'm trying to render using an EditorFor template, and I'm trying to apply formatting using the DisplayFormat attribute. However, it's not working at all -- it's totally being ignored.

Here is my template:

@model System.Decimal?
@Html.TextBoxFor(m => m)

Here is my model:

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

Here is my view:

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

But it's rendering a textbox with the following value:

189.9900

It seems pretty straight forward, but it's not working, and I have no idea why.

UPDATE: Just for kicks, I tried it with a DisplayFor template, and it worked:

@Html.DisplayFor(m => m.Retail)

So why would the DisplayFor template work, but not the EditorFor template, when I've set ApplyFormatInEditMode to true?

UPDATE 2: Never mind, the reason that worked is because my Decimal display template was hard-coded to format that way. So my display template also does not work.

解决方案

Darin Dimitrov posted this answer, and I was able to get it working using his solution:

@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)

A bit crude, IMO, that this doesn't work w/ TextBoxFor, but at least it works.

这篇关于DisplayFormat没有得到应用的十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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