问题
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