ASP.Net MVC 2 中可以有子编辑器吗?
我有一个自定义对象的编辑器模板。非常基本 - 只有一堆字符串和日期:
<%: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经在 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 yourComplexModel.ascx
editor and within yourComplexModel.ascx
you callHtml.EditorFor(m => m.ADate)
.I'm not sure about updates to MVC 2 with regards to this but MVC 3 it definitely works.
我找到了问题:
我为模型中的数据注释设置了部分元数据类(我正在使用实体框架)。我的部分日期视图适用于 ?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.