asp.net mvc4剃刀通视图模型来帮手 [英] asp.net mvc4 razor pass view model to helper

查看:98
本文介绍了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.

http://msdn.microsoft.com /en-us/library/ms379564(v=vs.80).aspx

编辑:

下面是一个例子...

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剃刀通视图模型来帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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