【问题标题】:MVC3 TextBoxFor vs. EditorFor - issues with eachMVC3 TextBoxFor vs. EditorFor - 每个问题
【发布时间】:2012-06-04 02:17:12
【问题描述】:

我在网上浏览了一段时间,但找不到针对我的具体问题的好的解决方案。我正在开发 MVC3 Razor 应用程序。我也在客户端用 jQuery 做很多事情,所以在这种情况下,我需要我的输入字段具有 html Id 属性。

在这个特定问题中,当我使用 TextBoxFor 显示货币(类型为十进制)时,该值呈现 4 个小数位(我只想要 2 个)。我输入了model属性

[DisplayFormat(DataFormatString = "{0:n2}", ApplyFormatInEditMode = true)]

当它呈现时,它似乎忽略了这个“displayFormat”。我在网上看到其他帖子确认 TextBoxFor html 帮助程序存在此问题。

另一方面,我可以更改为使用 EditorFor html 帮助程序,并且 DisplayFormat 模型属性适用于将我的文本框值格式化为小数点后 2 位。不利的一面是我失去了将 html 属性应用到 jQuery 所需的 html 助手的能力。什么是两全其美的最佳方式?我见过很多变通方法,但似乎必须有更简单的方法来做到这一点。

附带说明一下,我的特定项目要求我的大部分字段都具有 html Id 属性,有时还需要类,以便我可以执行客户端 jQuery 操作。不确定这是否意味着我应该专注于扩展 EditorFor html 帮助器以也接受 html 属性。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-3 razor html-helper modelattribute


    【解决方案1】:

    DisplayFormat 仅由 MVC 在剃须刀的任何 Display 扩展方法中使用。 (例如DisplayFor)不幸的是,要执行您想要的操作,您必须编写一些“手动”代码……这里有一个示例可以帮助您进行操作。它基本上复制了 TextBoxFor 代码,但做出了一些很可能对生产不利的假设。请注意下面displayText 的分配,这就是我们真正关心您在DisplayFormat 中设置的DisplayFormatString 的地方。

    在你的视野中称呼它@Html.TextEditorFor(m => m.Value)

    public static class CustomEditorFor
    {
        public static IHtmlString TextEditorFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes)
        {
            return TextEditorFor(helper, expression, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }
    
        public static IHtmlString TextEditorFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IDictionary<string, object> htmlAttributes)
        {
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            object value = metadata.Model;
            string displayText = string.Format(metadata.DisplayFormatString, value);
    
            string fullName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(ExpressionHelper.GetExpressionText(expression));
    
            TagBuilder tagBuilder = new TagBuilder("input");
            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("type", "text");
            tagBuilder.MergeAttribute("name", fullName, true);
    
            tagBuilder.MergeAttribute("value", displayText);
            tagBuilder.GenerateId(fullName);
            ModelState modelState;
            if (helper.ViewData.ModelState.TryGetValue(fullName, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                }
            }
    
            tagBuilder.MergeAttributes(helper.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata));
    
            return new HtmlString(tagBuilder.ToString(TagRenderMode.SelfClosing));
    
        }
    }
    

    【讨论】:

      【解决方案2】:

      这里的简单解决方案是为您的自定义货币类型创建自定义显示和编辑器模板。这样做。

      public class MyModel {
          [DataType(DataType.Currency)]
          public decimal mycurrency {get;set;}
      }
      

      然后在 ~/Views/Shared/EditorTemplates/Currency.cshtml(或 DisplayTemplates)中

      @model decimal?
      
      @Html.TextBox("", string.Format("{0:0.00}", Model), new { whateveryourattributes="attributevalues"})
      

      然后你可以只使用 DisplayFor 或 EditorFor 或其他任何东西。

      【讨论】:

        【解决方案3】:

        我最终使用了 Telerik 的数字文本框控件。我确信上述两个答案都会修复十进制格式。

        我仍然需要回到模型验证上。我遇到了 EditorFor html 帮助程序拉动验证模型上的数据注释以进行验证的问题,而对 TextBoxFor 执行相同操作则没有。但与此同时,EditorFor 助手的缺点不接受我需要 jQuery 的 HtmlAttributes。

        我已经开始使用 MVC4 RC,所以我希望其中许多问题在版本 4 中得到修复。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-09-14
          • 2012-03-01
          • 2021-05-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多