AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Please give an example of returning a Partial View from a Razor Page method

Open RyanTaite opened this issue 8 years ago • 11 comments

Born from this issue: #8132

Currently, I only know how to return a Partial View in a .Net Core 2.1 Razor Pages web app by using a Controller. I wrote an example project of this here: https://github.com/RyanTaite/DynamicListExample

I would like to know how this can be done from a Razor Page method instead.

Copying from my comment on the above issue:

In Startup.cs's Configure method I had to add: app.UseMvc(routes => routes.MapRoute("default", "{controller}/{action=Index}/{id?}"));

Then I add a Controller:

using DynamicListExample.Models;
using Microsoft.AspNetCore.Mvc;

namespace DynamicListExample.Controllers
{
    public class ParentController : Controller
    {
        public IActionResult GetChildPartialView()
        {
            return PartialView("_ChildPartialView", new Child());
        }
    }
}

And I can call that with a @Url.Action("GetChildPartialView", "Parent") from my View/Ajax method

But, I would like to point out that PartialView("_ChildPartialView", new Child()); throws an error saying:

Cannot resolve partial view '_ChildPartialView'

It actually can find and return _ChildPartialView though. So there will be an error for every PartialView("_somePartialView", ...) method call that you just have to ignore

Changing it to PartialView("Shared/_ChildPartialView", ...) gets rid of the error in Visual Studio, but fails to locate the View when running the application because it's looking for it in a View folder that doesn't exist. I don't know how to override that behavior.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

RyanTaite avatar Aug 20 '18 19:08 RyanTaite

@rynowak please review.

Rick-Anderson avatar Aug 22 '18 16:08 Rick-Anderson

@RyanTaite did the samples in #8132 help? Would you be able to help @guardrex getting this documented?

Rick-Anderson avatar Aug 23 '18 17:08 Rick-Anderson

@RyanTaite, you've posted some great info, thanks. Let me hack around with it, too, and we can compare notes. I should be able to get to this soon ... early next week.

guardrex avatar Aug 23 '18 17:08 guardrex

@Rick-Anderson Yes, pranavkm's examples seem to be exactly what I was after in regards to returning a Partial View from a Razor Page

@guardrex I'm happy to help with updating the documentation. I'll see what I can do later, I think I would also be in the early next week timeline.

RyanTaite avatar Aug 23 '18 17:08 RyanTaite

@RyanTaite Sure thing. Let me know if you decide that you want to write the section or just review what I write to make sure that I'm covering the bases. I just saw the examples, too. I think we'll be in good shape getting this covered next week.

guardrex avatar Aug 23 '18 17:08 guardrex

@guardrex I think I'm going to lean towards reviewing your write up, since I've really only done very small updates here-and-there to the docs as I've walked-through tutorials. That said, I'll still try to write up my own to compare against yours and learn how to make larger edits like this.

Hope that's alright.

RyanTaite avatar Aug 23 '18 18:08 RyanTaite

Sure ... np. I envision this will be a section added to this topic (I think). I'll know more when I take a closer look. The section will include versioning ("moniker ranges") to display content for 2.1 vs. 2.2 or later. It looks like there isn't a 2.0 workaround according to what was posted on the other issue.

I'll be sure to ping you on the PR. At the current rate of work, I think I'll be on this by Tuesday/Wednesday next week ..... if everything goes smoothly. 🏃🏃🏃🏃😅

guardrex avatar Aug 23 '18 18:08 guardrex

http://www.hishambinateya.com/introducing-partial-action-result-helper-in-razor-pages

Rick-Anderson avatar Aug 23 '18 18:08 Rick-Anderson

@Rick-Anderson @scottaddie The topic ...

Partial views in ASP.NET Core https://docs.microsoft.com/aspnet/core/mvc/views/partial

... focuses on MVC views ... really heavily focuses on views with lots of "MVC" and "view" language. We have two options:

  • Try to weave RP pages into this. Samples have a bearing on this: There will be 1-3 RP samples depending on supported approaches/versions. (More on that in a sec.)
  • Create a companion Partial views in ASP.NET Core Razor Pages (that sits under the RP node in the TOC). Perhaps, change the name of this topic to Partial views in ASP.NET Core MVC. There will be some duplication, especially in the introduction, but it will be easier to focus the text and code samples on RP and deal with the RP sample(s).

If we're going to have two or three RP samples/approaches for this, Option 2 looks better to me. If we only go with one 2.2 sample, then perhaps Option 1 is the way to go.

There are existing samples for 2.1 and 2.2. A few options are ...

  • Create a 2.1 AND 2.2 sample. Possibly include 2.0, if workable. Show both (or three) approaches in the topic.
  • Create a 2.1 AND 2.2 sample. Possibly include 2.0, if workable. Only show/discuss the 2.2 or later approach, which is the way we want cats to go. Near the link to the samples folder at the top, state that there are differences between 2.0/2.1 and 2.2 approaches and that we recommend upgrading a 2.0/2.1 app to 2.2 or later to take advantage of the sweeter approach.
  • Only create and cover a 2.2 sample and version this topic for 2.2 or later.

Note that I don't have a 2.0 approach. See: https://github.com/aspnet/Docs/issues/8132#issuecomment-415480829

I'm leaning in the direction of one 2.2 sample and one topic that covers both MVC and RP (leave it under MVC and link it under the RP TOC node). That's the greatest bang for the buck in terms of content generation and maintenance costs. 💰 However, let me know what you prefer.

guardrex avatar Sep 04 '18 15:09 guardrex

@guardrex I'm in favor of 1 topic that covers both MVC and RP. The single 2.2 sample should work too.

scottaddie avatar Sep 06 '18 20:09 scottaddie

@scottaddie Agreed. This is a good Saturday morning project for me. I'm looking forward to it! :smile:

guardrex avatar Sep 06 '18 20:09 guardrex