格式化@ Html.EditorFor

时间:2018-10-09 15:36:05

标签: c# asp.net asp.net-mvc formatting

我正在尝试在编辑表单上设置字段格式。我希望它是没有小数的货币格式。

    <div class="col-md-3">
            @Html.LabelFor(model => model.Value, htmlAttributes: new { @class = "control-label col-md-3 noleftpadding" })
            <div class=" input-group col-md-6 nopadding">
                <div class="col-md-12 nopadding">
                    @Html.EditorFor(model => model.Value, new { htmlAttributes = new { @class = "form-control" } })
                </div>
            </div>
        </div>

我将如何处理?

我也尝试过更改模型,但是当我执行此方法时,每次我尝试编辑另一个字段时,即使我没有故意进行此更改,应用程序也会将现有值重置为null。

    [DisplayFormat(DataFormatString = "{0:C0}", ApplyFormatInEditMode = true)]
    public Nullable<decimal> Value { get; set; }

我发现,如果将ApplyFormatInEditMode设置为false,它不会重置我的值,但是在编辑页面的显示中,当我希望它看起来像没有小数的货币值时,它又返回到十进制值。 / p>

编辑器模板

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - My ASP.NET Application</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <script type = 'text/javascript' >
        var appInsights=window.appInsights||function(config)
        {
            function r(config){ t[config] = function(){ var i = arguments; t.queue.push(function(){ t[config].apply(t, i)})} }
            var t = { config:config},u=document,e=window,o='script',s=u.createElement(o),i,f;for(s.src=config.url||'//az416426.vo.msecnd.net/scripts/a/ai.0.js',u.getElementsByTagName(o)[0].parentNode.appendChild(s),t.cookie=u.cookie,t.queue=[],i=['Event','Exception','Metric','PageView','Trace','Ajax'];i.length;)r('track'+i.pop());return r('setAuthenticatedUserContext'),r('clearAuthenticatedUserContext'),config.disableExceptionTracking||(i='onerror',r('_'+i),f=e[i],e[i]=function(config, r, u, e, o) { var s = f && f(config, r, u, e, o); return s !== !0 && t['_' + i](config, r, u, e, o),s}),t
        }({
            instrumentationKey:'7b294d9c-2c13-4d61-bd3f-65ffb8e1561a'
        });

        window.appInsights=appInsights;
        appInsights.trackPageView();
    </script>
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("SJAssoc", "LoggedIn", "Account", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("To Do List", "Index", "Todoes")</li>
                    <li>@Html.ActionLink("Strategy", "Index", "Strategy")</li>
                    <li>@Html.ActionLink("Opptys", "Index", "Opptys")</li>
                    <li>@Html.ActionLink("EMeetings", "Index", "EMeetings")</li>
                    <li>@Html.ActionLink("Register", "Register", "Account")</li>
                    <li>@Html.ActionLink("Logout", "Logout", "Account")</li>
                    @*<li>@Html.ActionLink(""</li>*@

                </ul>

            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

0 个答案:

没有答案