本文介绍了asp.net mvc4剃刀通视图模型来帮手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要视图模型传递给我的HTML帮助/
我曾尝试
I want to pass view model to my html helper/I have tried
public static string GenerateFullTable(this HtmlHelper helper, IEnumerable<CarsViewModel> model)
{
但我不知道哪种模式会。
But I dont know which model would be.
是否有可能使万能帮手它得到会得到不同的视图模型?
Does it possible to make universal helper which gets would get different view models?
推荐答案
是的,这就是所谓的仿制药。
Yes, it's called Generics.
编辑:
下面是一个例子...
Here's one example...
public static string GenerateFullTable<T>(this HtmlHelper helper, IEnumerable<T> model)
{
...
}
您可以进一步约束T可将特定类型或继承某个接口,也许是这样的:
You can further constrain T to be of a specific type or inheriting certain interface, maybe something like this:
public static string GenerateFullTable<T>(this HtmlHelper helper, IEnumerable<T> model) where T : MyModelsInterface
{
}
但是,这取决于你的需要。希望这有助于;)
But that depends on your needs. Hope this helps ;)
这篇关于asp.net mvc4剃刀通视图模型来帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!