开发者

html helpers for 'decimal' type and formatting?

开发者 https://www.devze.com 2023-02-03 09:18 出处:网络
property: public decimal Cost { get; set; } html helper: <%: Html.TextBoxFor(m => m.Cost)%> Question: when I am setting the Cost property, how do开发者_开发问答 I format it? for example s

property:

public decimal Cost { get; set; }

html helper:

<%: Html.TextBoxFor(m => m.Cost)%>

Question: when I am setting the Cost property, how do开发者_开发问答 I format it? for example show a precision of two decimal points?


I've tweaked Jamiec's answer above a little to (a) make it compile and (b) use the same underlying methods as the framework does:

public static MvcHtmlString DecimalBoxFor<TModel>(this HtmlHelper<TModel> html, Expression<Func<TModel, decimal?>> expression, string format, object htmlAttributes = null)
{
    var name = ExpressionHelper.GetExpressionText(expression);

    decimal? dec = expression.Compile().Invoke(html.ViewData.Model);

    // Here you can format value as you wish
    var value = dec.HasValue ? (!string.IsNullOrEmpty(format) ? dec.Value.ToString(format) : dec.Value.ToString())
                : "";

    return html.TextBox(name, value, htmlAttributes);
}


I recommend DisplayFor/EditorFor template helper.

// model class
public class CostModel {
  [DisplayFormat(DataFormatString = "{0:0.00}")]
  public decimal Cost {get;set;}
}

// action method
public ActionResult Cost(){
  return View(new CostModel{ Cost=12.3456})
}

// Cost view cshtml
@model CostModel

<div>@Html.DisplayFor(m=>m.Cost)</div>
<div>@Html.EditorFor(m=>m.Cost)</div>

// rendering html
<div>12.34</div>
<div><input class="text-box single-line" id="Cost" name="Cost" type="text" value="12.34" /></div>

Hope this help.


You could define your own extension method, something like:

public static MvcHtmlString DecimalBoxFor<TEntity>(
            this HtmlHelper helper,
            TEntity model,
            Expression<Func<TEntity, Decimal?>> property,
            string formatString)
        {
            decimal? dec = property.Compile().Invoke(model);

            // Here you can format value as you wish
            var value = !string.IsNullOrEmpty(formatString) ? 
                              dec.Value.ToString(formatString) 
                            : dec.Value.ToString();
            var name = ExpressionParseHelper.GetPropertyPath(property);

            return helper.TextBox(name, value);
        }

And then usage would be:

<%Html.DecimalBoxFor(Model,m => m.Cost,"0.00")%>


The solution to the described problem is quite simple: you just have to apply the following attribute to your addressed model class property:

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

Where DataFormatString describes your desired display format and the ApplyFormatInEditMode flag indicates that you also want to apply this formatting in editable mode, too (and not only in read-only mode, which is the case if you omit this).

(See also DisplayFormatAttribute Class)


I dont think there is a way to do it using the HTML helper, however you could send the value for the textbox pre-formatted with the 2 decimal precision.


If using the For method is not a must, you can simply do like this.

<%: Html.TextBox("Cost", Model.Cost.ToString("N2")) %>
0

精彩评论

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

关注公众号