定制EditorTemplate不在MVC4被用于DataType.Date [英] Custom EditorTemplate not being used in MVC4 for DataType.Date

查看:159
本文介绍了定制EditorTemplate不在MVC4被用于DataType.Date的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是微软<指令/一>。一切都相当顺利 - 除了我的几个日期模特属性现在都呈现不同。例如,这些属性中的一个视图模型的定义是这样的:

I'm upgrading an MVC3 application to MVC4 using the instructions from Microsoft. Everything went fairly smoothly - except a few of my date model properties are now rendering differently. For example, one of these properties is defined in the viewmodel like this:

[Required(ErrorMessage = "Required")]
[DataType(DataType.Date)]
[RegularExpression(@"([1-9]|0[1-9]|1[012])...",
    ErrorMessage = "Format is mm/dd/yyyy")]
[FormatHint("mm/dd/yyyy")]
[InputSize("small")]
public string Date { get; set; }

升级到MVC4之前,这将可以通过调用呈现 @ Html.EditorFor(M = GT; m.Date)这将使用自定义EditorTemplate - 在 String.cshtml 模板(因为它是一个字符串!)。我有一个格式化的HTML,所以它利用一个领域的布局,jQueryUI的一些自定义的数据标注,并在客户端Twitter的引导。验证通过jQuery验证不显眼的完成。总之,这是怎么回事previously呈现:

Before upgrading to MVC4, this would be rendered via calling @Html.EditorFor(m => m.Date) which would use a custom EditorTemplate - the String.cshtml template (since it's a string!). I have some custom data annotations that formats the html so it utilizes a field layout, jQueryUI, and twitter Bootstrap on the client side. The validation is done via jquery validation unobtrusive. Anyhow, this is how it previously rendered:

现在,我使用MVC4,在 String.cshtml 编辑模板不会被调用该物业的任何更长的时间。它使得像这样(在使用Chrome的HTML5编辑器的东西,我假设):

Now that I'm using MVC4, the String.cshtml editor template is not being called for this property any longer. It renders like this (in Chrome using the HTML5 editor stuff, I assume):

输入元素看起来pretty大同小异 - 所有的jQuery验证位在那里 - 唯一的区别似乎是键入属性现在键入=日期,在之前它是键入=文字

The input element looks pretty much the same - all the jQuery validation bits are in there - the only difference seems to be the type attribute is now type="date", where before it was type="text".

我想继续使用 String.cshtml EditorTemplate此数据类型。我想有可能是一个数据的注释,我可以把视图模型属性,为 @ Html.EditorFor一个TemplateHint(...)。如果不是这样,我想知道,我可以写劫持MVC4的格式自定义EditorTemplate(我已经试过 DateTime.cshtml - 它没有被称为其一) 。如果既没有这些的话,我愿意就如何让我的财产渲染像以前那样的建议。

I'd like to continue using the String.cshtml EditorTemplate for this datatype. I'm thinking there might be a data annotation that I can put on the ViewModel property to provide a TemplateHint for @Html.EditorFor(...). If not this, I'd like to know the custom EditorTemplate that I can write to hijack MVC4's formatting (I've already tried DateTime.cshtml - it's not being called either). If not either of those, then I'm open to suggestions on how to get my property rendering like it used to.

推荐答案

MVC4 ,所用的模板从determinated:

In MVC4, the used template is determinated from :


  1. UIHintAttribute 如果任何

  2. DataTypeAttribute 如果任何

  3. 的类型属性

  1. The UIHintAttribute if any
  2. The DataTypeAttribute if any
  3. The type of the property

MVC3 DataTypeAttribute 未使用。

您酒店有

[DataType(DataType.Date)]

所以模板用于 Date.cshtml 。如果不存在,它,默认渲染被执行。

so the template Date.cshtml is used. If it does not exists, default rendering is executed.

您有两个选项来解决问题:

You have two options to resolve your problem :


  1. 添加 UIHint(字符串)你的财产,或

  2. String.cshtml 编辑器模板重命名为 Date.cshtml

  1. Add a UIHint("String") on your property, or
  2. Rename your editor template from String.cshtml to Date.cshtml

这篇关于定制EditorTemplate不在MVC4被用于DataType.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆