ASP.Net MVC: Calling a method from a view

前端 未结 8 1290
轻奢々
轻奢々 2020-11-29 02:52

In my MVC app the controller gets the data (model) from an external API (so there is no model class being used) and passes that to the view. The data (model) has a container

8条回答
  •  半阙折子戏
    2020-11-29 03:06

    Building on Amine's answer, create a helper like:

    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString CurrencyFormat(this HtmlHelper helper, string value)
        {
            var result = string.Format("{0:C2}", value);
            return new MvcHtmlString(result);
        }
    }
    

    in your view: use @Html.CurrencyFormat(model.value)

    If you are doing simple formating like Standard Numeric Formats, then simple use string.Format() in your view like in the helper example above:

    @string.Format("{0:C2}", model.value)
    

提交回复
热议问题