How to create a helper that will display a partial view using a model?

I will give a very simple example:

at the moment I have to write like this:

<% Html.RenderPartial("hello", new HelloInput { Name = "Jimmy" } ); %>

I would like to be able to:

<%=Html.Hello("Jimmy") %>

So, I would like to know how to create this helper:

public static string Hello(this HtmlHelper helper, string name)
{
    return the result of rendering partial view "hello" with HelloInput{ Name = name };
}
+3
source share
1 answer

Partial is the version <% = of the RenderPartial version:

public static string Hello(this HtmlHelper helper, string name)
{
    return helper.Partial("hello", new HelloInput { Name = name } );
}
+2
source

Source: https://habr.com/ru/post/1764943/


All Articles