I'm looking to migrate a fairly complex ASP.NET MVC application from full-framework to .Net Core, and updating to the latest patterns. By the time we're done, .Net 5 will be fully released....
Our existing application has a Layout view (_Layout.cshtml) which renders the main content. It also has different "sections" that display other data (often having their own AJAX functionality).
For loading these sections (in the old application), we use the following example syntax in the _Layout.cshtml file:
<div class="....">
@this.Html.Action("ActionName", "ControllerName")
</div>
When this controller action is called, it pulls data from various sources, generates a ViewModel, and returns a View (consuming this VM) that then displays on the layout page. These sections are "self contained" - often having their own AJAX activity.
For MVC .Net Core 3.x/5.x, I've seen plenty of examples where a partial view is rendered using <partial name="_NameOfPartial" />. This has an attribute for model, so I could use this approach, but from a "single responsibility" perspective it makes little sense to me for something unrelated (in this case, my Layout) to know anything about the ViewModel for an un-related section. What I've not seen is an example to do this by calling the Controller first to generate the required ViewModel...
I'm aware of (but not greatly familiar with) razor pages (vs razor views). My first thought was that they're no doubt useful for "light weight" applications, but maybe not useful for an application as complex as mine. But, am I mis-interpreting this - what criteria dictates whether to use razor pages or razor views? Is "MVC" out-dated?
However, my main question is really in two parts:
- For my scenario, for Core 5.x, is it "best practice" for a View (not just the Layout) to call a Controller Action method to generate a View Model and return a partial view? If not, what is best practice here (consider this a complex application)?
- If "yes" to 1., then what is the syntax?
Thanks in advance.
ViewComponentthe way to go....? - DrGriff