开发者

Setting properties of textbox and formatting it in MVC 2

I'm using MVC 2. I want to format the data that I display in my textbox when I edit a record.

I display my textbox like this:

<%: Html.TextBoxFor(m => m.AnnualIncome, new { @class = "textbox", maxlength = "50", size = "15" })%>

How would I format this? I tried adding the String.Format from below but it does not work:

<%: Html.TextBoxFor(model => model.AnnualIncome, String.Format("{0:F}", Model.AnnualIncome)) %>

AnnualIncome is of type Nullable decimal.

EDIT:

I am putting in a thousands separator to give me a value like 1,000,000 just to see the result.

[DisplayFormat(ApplyFormatInEditMode = true, 开发者_运维百科DataFormatString = "{0:0,0}")]
public Nullable<decimal> AnnualIncome { get; set; }

Here is my HTML for the above:

<%: Html.TextBoxFor(m => m.AnnualIncome, new { @class = "textbox", maxlength = "50", size = "15" })%>

This still displays 1000000.00. I am trying to get it to display 1000000 in the textbox when the view is populated.


Put a DisplayFormatAttribute.DataFormatString attribute on the property:

[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:F}")]
public float AnnualIncome { get; set; };

Then you just do:

<%: Html.EditorFor(m => m.AnnualIncome) %>

To style the inputs, you can do:

input.editor-field {
    color: #781351;
}

...or whatever you prefer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜