Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Html.RenderAction in ASP.NET Core

I am trying to switch to ASP.NET Core from my small ASP.NET MVC 4 application.

In my MVC 4 application, I have a Layout file that uses RenderSection as:

@RenderSection("xyz", required: false) 

Then, in my Index file, I have:

@section xyz{         @{Html.RenderAction("abc");}     } 

So, I am calling controller action method abc() from Index. The method abc() passes a model object and returns a partial view with that model. I cannot use RenderPartial as it needs a model to be passed.

Now, in ASP.NET Core, I don't have RenderAction() method.

My question is: How would I invoke a controller action method from my Index file? Is there any other HTML helper for that (although I don't see any)?

.

like image 685
user6542823 Avatar asked Dec 28 '16 00:12

user6542823


People also ask

What is HTML RenderAction?

RenderAction(HtmlHelper, String) Invokes the specified child action method and renders the result inline in the parent view. RenderAction(HtmlHelper, String, Object) Invokes the specified child action method using the specified parameters and renders the result inline in the parent view.

What is the difference between RenderPartial and RenderAction?

RenderPartial is used to display a reusable part of within the same controller and RenderAction render an action from any controller. They both render the Html and doesn't provide a String for output.

What is HTML RenderPartial in MVC?

Html. RenderPartial. This method result will be directly written to the HTTP response stream means it used the same TextWriter object as used in the current webpage/template. This method returns void. Simple to use and no need to create any action.

What is partial view in asp net core?

A partial view is a Razor markup file ( . cshtml ) without an @page directive that renders HTML output within another markup file's rendered output. The term partial view is used when developing either an MVC app, where markup files are called views, or a Razor Pages app, where markup files are called pages.


2 Answers

I was finally able to do it with ViewComponent. So, instead of RenderAction(), I did:

@section xyz{         @await Component.InvokeAsync("abc")     } 

Where abc is a class as abcViewComponent. The ViewComponent looks like:

public class abcViewComponent : ViewComponent     {         private DbContextOptions<MyContext> db = new DbContextOptions<MyContext>();         public async Task<IViewComponentResult> InvokeAsync()         {             MyContext context = new MyContext(db);             IEnumerable<tableRowClass> mc = await context.tableRows.ToListAsync();             return View(mc);         }     } 

Then, I created a view under a new folder 'abc' as Views/Home/Components/abc/Default.cshtml

It is to be noted that the view name is Default.cshtml and that is how it worked. If anyone has any better solution, please let me know.

Thanks for pointing me in the direction of ViewComponent.

like image 70
user6542823 Avatar answered Sep 21 '22 10:09

user6542823


Just for completion of the above question, it is probably safer to pass the name of the ViewComponent class as shown below:

@section xyz{     @await Component.InvokeAsync(nameof(yournamespace.abc)) } 

and although it is common to use "Default" as the resulting view, you can also return the view name if that differs from default:

public class abcViewComponent : ViewComponent {     private DbContextOptions<MyContext> db = new DbContextOptions<MyContext>();     public async Task<IViewComponentResult> InvokeAsync()     {         MyContext context = new MyContext(db);         IEnumerable<tableRowClass> mc = await context.tableRows.ToListAsync();         return View("YourViewName", mc);     } } 
like image 34
Nick Parker Avatar answered Sep 18 '22 10:09

Nick Parker