Partial Pages in ASP.NET Core 2.0 - c#

TL; DR Are "partial Pages" possible in ASP.NET Core 2.0? If so, why am I getting the error described below?
I've been messing with the new Razor Page feature of ASP.NET 2.0 and having some trouble with partials. The documentation linked above refers to "partials" in several places, but only to the extent of saying mystical things like "The layouts, templates, and partials you're using with MVC controllers and conventional Razor views just work." Do they mean partial Views, like what we're used to, or some new kind of partial Page also? They show that files with the conventional Partial suffix in the name can be placed in the Pages/ folder, but the files that they mention (e.g., _ValidationScriptsPartial.cshtml) don't have the #page declaration. If "partial Pages" are indeed a new thing, then I would expect the following minimal scenario to work:
A Page file named Derp.cshtml:
#page
#namespace MyApp.Pages
#model DerpModel
<p>Member 1: #Model.Member1</p>
<p>Member 2: #Model.Member2</p>
A class file named Derp.cshtml.cs in the same directory (nested under the first in VS 2017's Solution Explorer), containing the following class:
namespace MyApp.Pages {
public class DerpModel : PageModel {
public void OnGet() {
Member1 = "Derp Member1";
Member2 = "Derp Member2";
}
public string Member1 { get; set; }
public string Member2 { get; set; }
}
}
A second Page file named DerpWrapper.cshtml in the same directory, with the following code:
#page "{id}"
#namespace MyApp.Pages
#model DerpModel
<p>Outer Member 1: #Model.Member1</p>
<p>Outer Member 2: #Model.Member2</p>
#Html.Partial("Derp", Model)
And of course a _ViewImports file that declares #namespace MyApp.Pages
Note that both Razor Pages have the same #model type, and the second Page basically wraps up the first one with a call to Html.Partial() and passes its Model. This should all be pretty standard stuff, but upon navigating to http://localhost:xxx/DerpWrapper, I get a 500 response due to a NullReferenceException with the following stack trace.
MyApp.Pages.Derp_Page.get_Model()
MyApp.Pages.Derp_Page+<ExecuteAsync>d__0.MoveNext() in Derp.cshtml
+
<p>Inner Member1: #Model.Member1</p>
...
MyApp.Pages.DerpWrapper_Page+<ExecuteAsync>d__0.MoveNext() in DerpWrapper.cshtml
+
#Html.Partial("Derp", Model)
...
Why does the "wrapper" Page's Model equal null? Is there no way to define a Razor Page and display it from another Page or View with Html.Partial()?

Razor’s partial views are basically .cshtml views that are transcluded into another view, that is a razor view or a razor page. They are rendered within that view and as such inherit some of the view’s properties, e.g. its ViewData. They however do not have a controller, a page model or some other kind of “code behind” that gives you a way to run code for the partial.
As such, the primary use for partials is to extract common components that do not contain logic on their own into separate and reusable files.
The other kind of reusable components that exist in Razor are view components. View components are similar to partials but they do have an actual class that backs them and that allows you to add your own logic to the view component.
At the moment, view components follow the MVC layout for views, meaning the view component is actually a class and you return a view from its Invoke method which causes the view engine to locate that .cshtml in the Views folder. David Fowler confirmed to me though, that they are working on a Razor page like experience for view components too, so in a later version of ASP.NET Core, you will eventually be able to write a .cshtml view component and add some code-behind to it (just like Razor pages).
Coming back to your question though, at the moment there are two kinds of entry points for an MVC route: An MVC controller, possibly returning a view, and a Razor page. Neither of these can invoke one or the other inside of them though. If you are rendering a view, you can load partials or view components but not other “full” views or Razor pages. And if you’re rending a Razor page, you also can load partials or view components, but still no other “full” views or Razor pages.
So what you want to do is simply not possible because a Razor page is supposed to be an entry point to the route, and while invoking any view as a partial may work (in some situations), you will certainly neither get the controller for that view nor the page model running for it. If you want to invoke something that has complex logic, you should use view components instead.

Answers on Stack overflow remain for ages so it's important that we ensure that they are improved.
To add onto Poke's Answer or rather to correct it. Partial views separate common sections between pages. These sections can include logic. But that logic should not involve any field(variables, or objects) contained or defined within any specific page. They can apply logic though dependency injection... by injecting them into the partial view.

Related

Sharing pages between different Razor Page areas

I wonder if I could get some ideas on how best to approach this.
I have a Razor Pages application. I have several areas, and each area has its own layout page and menus.
This is pretty straight forward, but I have a couple of pages I need to share with two or more areas. These shared pages will be passed an argument that indicates which area it's being used by, and I would want the layout page (menu) for that page to be the one for that area. (Ideally, the URL would also reflect the current area.)
I'm not sure if this is practical. I'd like to keep things as simple as possible. But all I come up with is some fairly complex routing stuff.
Two options I can think of:
Put the shared page in the root Pages folder and use AddPageRoute to add multiple routes to the shared page including a parameter for the area name:
options.Conventions.AddPageRoute("/SharedPage", "{areaName}/alias1");
options.Conventions.AddPageRoute("/SharedPage", "{areaName}/alias2");
You can set the layout based on the value of RouteData.Values["areaName"]. Don't use area as the parameter name. It's a reserved word as far as routing is concerned:
#{
if(RouteData.Values["areaName"] == "foo")
{
Layout = "_FooLayout";
}
if(RouteData.Values["areaName"] == "bar")
{
Layout = "_BarLayout";
}
}
Add pages to the areas that act as placeholders to generate the routes, and then extract the body of the shared page to a partial that can be placed in the top level Pages/Shared folder so that it is accessible to all other pages in the application. If you want to centralise some of the processing for the shared page, create a class that inherits from PageModel and put your processing logic there, then have the actual PageModel classes inherit from that.
You can make this by using partial views.
So, create one patial view and place it not in areas (in Common for example or other folders).
In areas folder create master view that render only your Partial view.

How to create reusable control in ASP.NET MVC

How can/should I create some "custom control" in ASP.NET MVC 3? I have red about partial views, ViewUsersControl, Html.RenderAction, but I still don't know, which way is the proper MVC way for razor views.
If I need to render some ajax component to view, I can imagine to do it by partial view, but what if I want to render section with custom logic?
1) PartialViews
2) Custom Html helpers
3) Child Actions
Update ASP.NET Core:
2) Tag Helpers are preferred way over Custom Html helpers
3) View Components are used instead of Child Actions
You may use
#{Html.RenderPartial("YourCustomView",YourModel);}
For instance, In your Shared folder create a new View and name it "_MyCustomControl"
Then in the code write :
#model YourNameSpace.Models.YourModel
#{
Layout = null;
}
#*Here write your control's markup*#
Then in your Views where you want to use this "control" you add:
#{Html.RenderPartial("_MyCustomControl",new YourModel { args });}
If you get into trouble with RenderPartial check this out
In addition to all the other answers for this post, I can offer you the use of EditorFor and DisplayFor templates. These are useful when you want to easily render or edit a custom type. It'll handle validation nicely (which can get weird when using partials) and you can nest them recursively (again another feature that isn't obviously handy until you need it).
You can also use Html.RenderAction() or Html.Action() to call another controller action within a view and display the results in-line in the page. This is probably the closest to what you need as it allows you to render a partial, include code in the controller and it also allows for the passing of parameters.
Links to:
DisplayFor and EditorFor Templates
Action and RenderAction
As you have mentioned that you can use Partial Views.
Yes you can use Partial Views, which is the most effective and efficient way.
For Ajax rendering you can always use
#using (Ajax.BeginForm("Details", new { id = Model.Post.PostId }, new AjaxOptions
{
Few of the links you would like to see
Rendering partial view in ASP.Net MVC3 Razor using Ajax
Render Partial Views using JQuery in MVC3

Can I populate a ContentPlaceHolder in a master page from within a Razor Partial View?

I'm using the typical built in view engine in mvc3 (is there a proper name for it?) for views and master pages and it's including a Razor partial view on the .aspx page. In the masterpage, there is a ContentPlaceHolder with an ID of "ScriptContent".
I want to be able to fill that ContentPlaceHolder from within the Razor partial view but I don't think this is possible. Does anyone know if it is possible and how I would go about doing that?
I already tried rendering it in the partial like so, but that didn't work.
#section ScriptContent {
... content ...
}
It would be very difficult, so much so that I'd recommend finding another way :(. I wish it was easier, but these are the complexities of integrating a new view engine into an existing legacy system.
To give you a head start if you really want to try it: You'd probably need to create a custom base class inheriting from WebViewPage for your Razor content pages, override some of the methods (honestly I'm not too familiar with that aspect so you'd need to debug to follow the pipeline) so that instead of treating the Layout property as the path to a Layout page, you treat it as a Master page. Then you'd need to instantiate the master page and somehow convert the Sections (which were transformed into calls to DefineSection by the Razor parser, and should be stored in a Dictionary somewhere on the base class) in to Content controls and stuff them in the Master Page.
If I haven't boggled your mind by this point, you may just be able to pull this off, but to be honest, I'd avoid it.
P.S. We refer to the older view engine as "ASPX", based on its file extension ;).

resolving model conflict between page and partial view in master page MVC?

I have a mvc site where the master page uses RenderPartial to return different views based on user roles. One of the partial view is inherited on a "SettingsModel" type. This results in a conflict with the underlying page which takes a different model. When it tries to render the page, it says "I'm expecting "settingsModel" but the incoming model is for the page. How can I resolve this conflict?
you can use RenderAction method to render different partial pages having their own view models
this article explains difference b/w rederpartial and renderAction with pros and cons of each

Could somebody explain ASP.NET MVC to me?

I'm trying to get into MVC and currently reading the wrox professional ASP.NET MVC book.
I kind of get it so far. Instead of each URL going to a page it goes to a controller. The controller action then gets the data and decides what view to use.
I also understand that if I have a url like/product.aspx?id=100 then the controller would get the product details and merge them with the "show product" view.
Now here's the bit I don't get...
If my product page has other stuff on it, like a login box, a "top 10 products" section, list of categories etc. which may or not be used on other pages too then how would I include them and keep their code separate?
In the classic aspx model it would be simple. If my top 10 products appeared on every page then I would put it in a master page, but more likely I would make it into a user control if it was going to be used on some pages not others.
From what I understand of MVC so far, my products controller would have to get the top 10 products and so would any other controller that was producing a page with the top 10 products on it.
Confused. Please help.
From what I understand of MVC so far,
my products controller would have to
get the top 10 products and so would
any other controller that was
producing a page with the top 10
products on it.
Not necessarily. You can use Master pages with MVC, as well as Partials to compartmentalize re-usable view content.
This is a good article on using Partials.
http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/
Also, in your Top 10 products example, you could have that rendered by a child action:
[ChildActionOnly]
public ActionResult GetTopTenProducts()
{
var products = db.GetTopProducts(10);
return View(products);
}
You would then have a partial view (.ascx) called "GetTopProducts.ascx" that would be rendered when you call the GetTopTenProducts() action. Then in your Master page, or anywhere you wanted that Top 10 list to show up, you would call it like this:
<% Html.RenderAction("GetTopTenProducts") %>
You're close.
You're just forgetting that your controller is a class that can inherit from a base class.
You can create a base controller class that handles retrieving the top ten products, and then have any controllers that need that functionality inherit from the base class.
You then create a Master Page that uses a Partial View to render the top ten products passed to the View (from the base controller).
...and voila! No code repetition.
Please don't forget that in ASP.NET MVC you still can use MasterPage. With specific to ASP.NET MVC 2 you can display those 10 products using RenderAction which can render Action from any Controller you select.
In addition to that, you should get to know PartialView as well.
You can use master pages and in MVC partial views are synonymous with user controls. The difference being master page does not have a corresponding controller. You an solve with one of two ways.
When you use a partial view you can
pass a model in.
<%= Html.RenderPartial("PartialView",Model) %>
You can just do the work in the partial view, utilizing the code brackets <% %>.

Categories

Resources