R
R
Ruslan2019-05-01 09:57:50
ASP.NET
Ruslan, 2019-05-01 09:57:50

Is there a strongly typed way to reuse Razor code in ASP.Net MVC?

Hello!
I have partial actions in my project (ActionResult PartialAction(){ return PartialView(); }) and Razor views that I use in other views with calls to type:

Html.RenderAction("ActionName","Controller",new { x = 1, y=2, z=3})
Html.RenderPartial("~/Views/Controller/ViewName",new { a=10, b=20, c=30})

But I don’t really like this approach: firstly, it’s impossible to use IntelliSense, but you need to search for the desired method and all its parameters every time, and secondly, it’s hard to find all the places where a certain method is called (Shift + F12 does not work) .
It is appropriate, correct, and not harmful to wrap such partial views and child actions as extensions like this:
public static class HelperExtensions
{
    public static MvcHtmlString RenderABC(this HtmlHelper self, int a,int b, int c)
    {
        return self.Partial("~/Views/Controller/ViewName",new { a, b, c});
    }
}

Accordingly, in any representation, instead of
Html.RenderPartial("~/Views/Controller/ViewName",new { a=10, b=20, c=30})

we write and get additional convenience Once again, the question is: is this approach normal or is it a bicycle, a crutch, an anti-pattern, etc. Or is there already a neat way to style reusable Razor code?
Html.RenderABC(10,20,30)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question