ASP.Net MVC 2 中可以有子编辑器吗?

发布于 2024-09-24 09:35:46 字数 647 浏览 0 评论 0原文

我有一个自定义对象的编辑器模板。非常基本 - 只有一堆字符串和日期:

<%:Html.LabelFor(model => model.AString)%>
<%:Html.TextBoxFor(model => model.AString)%>

<%:Html.LabelFor(model => model.ADate)%>
<%:Html.EditorFor(model => model.ADate)%>

<%:Html.LabelFor(model => model.AnotherDate)%>
<%:String.Format("{0:d}", Model.AnotherDate)%>

它存储在 Shared/EditorTemplates 文件夹中。我还在同一位置有一个 DateTime.ascx 编辑器模板,用于覆盖所有日期时间字段。

正如您在上面的代码中看到的,其中一个日期字段使用 EditorFor 作为日期字段,而另一个则没有。然而,它们都呈现相同的效果,这让我相信日期时间编辑器没有被访问。我做错了什么吗?看来这应该是可以做到的。

我直接在视图中使用日期时间编辑器,效果很好。

感谢您的任何帮助或见解。

I have an editor template for a custom object. Pretty basic - just has a bunch of strings and dates:

<%:Html.LabelFor(model => model.AString)%>
<%:Html.TextBoxFor(model => model.AString)%>

<%:Html.LabelFor(model => model.ADate)%>
<%:Html.EditorFor(model => model.ADate)%>

<%:Html.LabelFor(model => model.AnotherDate)%>
<%:String.Format("{0:d}", Model.AnotherDate)%>

This is stored in the Shared/EditorTemplates folder. I also have a DateTime.ascx editor template in the same location, to override ALL datetime fields.

As you can see in the code above, one of the date fields uses an EditorFor for the date field, the other does not. However, they both render the same, which leads me to believe the DateTime editor is not being accessed. Is there anything I'm doing wrong? Seems like this should be possible to do.

I've used the DateTime editor directly in a view, and it works fine.

Thanks for any help or insight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-10-01 09:35:46

我已经在 MVC 2 和 MVC 3 下尝试过您的场景,并且在 MVC 3 下它按您的预期工作。

正如我所理解的,您的 Shared 中有 EditorTemplates,其中包含您的 ComplexModel.ascx 编辑器,并且在您的 ComplexModel.ascx 中您调用 Html.EditorFor(m => m.ADate)

我不确定 MVC 2 是否有这方面的更新,但 MVC 3 肯定有效。

I've tried your scenario under MVC 2 and MVC 3 and under MVC 3 it works as you expect.

Just so I understand correctly you have EditorTemplates in your Shared which contains your ComplexModel.ascx editor and within your ComplexModel.ascx you call Html.EditorFor(m => m.ADate).

I'm not sure about updates to MVC 2 with regards to this but MVC 3 it definitely works.

记忆里有你的影子 2024-10-01 09:35:46

我找到了问题:

我为模型中的数据注释设置了部分元数据类(我正在使用实体框架)。我的部分日期视图适用于 ?DateTime (DateTime.ascx)

我将所有日期的 DataType 属性设置为 [DataType(DataType.Date)] 而不是 [DataType(DataType.DateTime) )]

改变这个属性就解决了这一切。

I figured out the problem:

I have partial metadata classes set up for Data Annotations in the Model (I'm using entity framework). My partial Date view is for ?DateTime (DateTime.ascx)

I had the DataType attribute for all the dates set to [DataType(DataType.Date)] instead of [DataType(DataType.DateTime)].

Changing that attribute fixed it all up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文