0

I want to create a custom separated from page html-helper that would use partial views functionality, but for some reason Html.Partial("_partialName") doesn't work

Content of separated cshtml file in App_Code:

@helper MyHelper(IEnumerable<string> something)
{
    <div>@Html.Partial("_viewName", something)</div>   
}

How to just use Html.Partial like in Razor view page?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Yes Man
  • 321
  • 1
  • 5
  • 14
  • Define "doesn't work" please – Andrei Feb 04 '19 at 09:53
  • @Andrei My bad. Here is the error text "HtmlHelper doesn't contains definition for Partial, most suitable method PartialExtensions.Partial(HtmlHelper, string)". I know what it about, tryed several ways to use this extension method, but with no results :( – Yes Man Feb 04 '19 at 10:15

1 Answers1

0

You can't just call Html.Partial("_name", model); in a helper method. Partial is an extention method to HtmlHelper type and Html is a property of View (which inherits from WebViewPage<TModel>). You can check it by going to the view (or partial view) and navigating to the definition of Html.

Calling it from a helper does not make sense, thus it does not have an object which exposes an accessible property Html form WebViewPage<TModel> type. In fact it is an instance of CustomHtml class. Try writing @this. in both view and helper. You'll see how they are different.

Ayron
  • 46
  • 1
  • 4
  • I came to the same conclusions recently and deside to make an extension method to HtmlHelper that provides an Instance of itself. – Yes Man Feb 05 '19 at 08:11