English 中文(简体)
多指标类集调查
原标题:Format numbers in MVC view

在我看来,我有一个模式,我想以具体的方式加以编排。

[DisplayFormat(DataFormatString = "{### ##}")] 
 [Display(Name = "Postnr")] 
 public string CustomerZip;

认为:

@Html.DisplayFor(modelItem => item.CustomerZip) 

在亚洲开发银行,这些数值被储存为第##号,因此,我在尝试这一方法时发现了一个错误:“以正确的格式显示数据”。 我认为(或相当希望)数据表会为我调整方向。

赞赏就如何以最佳方式做到这一点提出的任何建议。

最佳回答

几个方面:

  1. The DataFormatString needs to use the {0:...} indexer style format string, like you d use in string.Format. So you d need to do something like [DisplayFormat(DataFormatString="{0:### ##}"]. If the braces are supposed to be literal, then you d use [DisplayFormat(DataFormatString="{{{0:### ##}}}")]. However...
  2. There is no such format character # for strings. That is what you d use for a numeric type. If you are storing the value as an integer, then you re in luck; just change the type of CustomerZip to int.
  3. If you are NOT storing the value as an integer, but as a string (as you are wont to do with postal codes, usually), then you ve got more of a problem. Like I said, you don t have any custom format options with strings. You d need to represent CustomerZip as some custom type that is IFormattable, which you would define.
问题回答

采用以下格式:

<代码>{0:###}>

见MSDN网页

• 采用“Raw”这样的功能:

@Html.Raw(项目.Valor.ToString(“##”)

它用于任何类型的数据。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签