ASP.NET MVC 3 通用显示模板
我刚刚开始使用 ASP.NET MVC 3 的项目。我正在现有的对象系统之上进行构建,因此我要做的第一件事就是为现有的各种类型定义显示和编辑器模板。
在 MVC 中是否可以使用通用参数定义 DisplayTemplate?例如,我们有一个 BitString
我目前正在使用 Razor 来表达我的观点,但我不介意与 ascx (或直接 C#,如果有办法做到这一点)混合和匹配来实现此目的,
谢谢
编辑: 我认为这可能是这个问题的重复......但它已经一年半了,所以也许现在有人有更好的答案? 通用部分视图:如何设置通用类作为模型?
I've just started a project using ASP.NET MVC 3. I'm building on top of an existing object system, so one of the first things I have to do is define display and editor templates for the various types that exist.
Is it possible in MVC to define a DisplayTemplate with a generic argument? For example, we have a BitString<T>
class which takes an enumeration as the generic argument and represents a list of options wrapping the supplied enumeration. I'm hoping I can define a single Display/Editor template that handles all BitString instances.
I'm currently using Razor for my views, but I don't mind mixing and matching with ascx (or straight C# if there is a way to do it) to achieve this
Thanks
EDIT:
I think this might be a dup of this question... But it's a year and a half old, so maybe someone has a better answer at this point?
Generic partial view: how to set a generic class as model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您描述的问题是泛型的基本原理。
即使
String
是Object
的子类,ICollection
在 MVC3 中,我通过执行以下操作来解决这个问题:
然后在您看来,
当您只需要常见的东西而不知道通用类型时。
当您需要通用类型数据时。
用例:
我创建一个“ModelContainer”类,在其中存储我的模型,以及可以部分显示页面的错误消息数组。由于部分可以在每个页面上使用,因此它不知道通用类型是什么,因此需要这种解决方法。
如果您尝试在不知道其类型的情况下访问通用数据,则无法使用此方法。希望这能解决您的问题。
The problem you're describing is a fundamental principal of generics.
ICollection<Object>
is not the base class ofICollection<String>
even ifString
is a child class ofObject
. This is done at compile time, so you basically get two different ICollection class definitions. Therefore they cannot be casted. (Smart people of SO please feel free to correct me on any inaccuracies)In MVC3 I have worked around this by doing the following:
Then in your view
When you need just the common stuff without knowing the generic type.
When you need the generic type data.
Use Case:
I create a "ModelContainer" class that stores my Model inside, together with an array of Error Messages that can be displayed the page in a partial. Since the partial can be used on every page, it does not know what the Generic type will be, so this workaround is needed.
This cannot be used if you are trying to access the generic data without knowing its type. Hopefully this solves your problem.
我同意达里尔的回答,但我只想添加一个小的改进。
然后在您看来执行以下操作:
I agree with Daryl's answer, but I would just add a small improvement.
Then in your view do the following:
不,如果通用类型未知,则不可能拥有具有通用类型的视图。您不能像这样定义模型:
必须知道
T
:话虽这么说,我建议您不要尝试重用旧系统中的某些模型来思考视图模型 您可能会将其放在适当的位置并将其传递给视图。
No, it is not possible to have views with generic type if this generic type is not known. You cannot define a model like this:
T
has to be known:This being said I would recommend you instead of trying to reuse some models you had in your old system to think of what view models you might put in place and which will be passed to the views.
这可能不太理想,但您应该能够使用
This might be less than ideal, but you should be able to use