ASP.NET MVC 4-way model from view to html helper

I want to pass my ViewModel (not IEnumerable) to my own html helper

I did on IEnumerable like this:

Helper:

public static IHtmlString GenerateTable<TModel, TValue>(this HtmlHelper<TModel> inHtml, IEnumerable<TValue> model)

View:

@Html.GenerateTable(Model)

But how can I pass a model that is not IEnumerable for the helper?

I tried this:

public static MvcHtmlString MyHelper<TModel>(this HtmlHelper<TModel> html, object obj)
        {}

but when I call it that

@Html.MyHelper(Model)

obj always NULL

+5
source share
2 answers

Try it like this:

public static MvcHtmlString MyHelper<TModel>(this HtmlHelper<TModel> htmlHelper)
{
    TModel model = htmlHelper.ViewData.Model;
    // TODO: do something with the model ...
}

and in your strongly typed form:

@model MyViewModel
@Html.MyHelper()
+10
source

, , ... , , @nemesv, , ...... ,

+2

All Articles