asp.net mvc4 razor pass view model to helper

白昼怎懂夜的黑 提交于 2019-12-13 04:19:59

问题


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?


回答1:


Yes, it's called Generics.

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

Edit:

Here's one example...

public static string GenerateFullTable<T>(this HtmlHelper helper, IEnumerable<T> model)
{
    ...
}

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 ;)



来源:https://stackoverflow.com/questions/10968560/asp-net-mvc4-razor-pass-view-model-to-helper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!