Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
RyanTaite opened this issue Aug 20, 2018 · 11 comments
Closed
Assignees
Labels
Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@RyanTaite
Copy link

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.

@Rick-Anderson
Copy link
Contributor

@rynowak please review.

@Rick-Anderson Rick-Anderson added Pri3 Source - Docs.ms Docs Customer feedback via GitHub Issue labels Aug 22, 2018
@Rick-Anderson Rick-Anderson added this to the Backlog milestone Aug 22, 2018
@Rick-Anderson Rick-Anderson added Pri1 and removed Pri3 labels Aug 23, 2018
@Rick-Anderson Rick-Anderson modified the milestones: Backlog, Sprint 141 (9/3 to 9/21) Aug 23, 2018
@Rick-Anderson
Copy link
Contributor

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

@guardrex
Copy link
Collaborator

@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.

@RyanTaite
Copy link
Author

@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.

@guardrex
Copy link
Collaborator

@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.

@RyanTaite
Copy link
Author

@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.

@guardrex
Copy link
Collaborator

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. 🏃🏃🏃🏃😅

@Rick-Anderson
Copy link
Contributor

@guardrex
Copy link
Collaborator

guardrex commented Sep 4, 2018

@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: #8132 (comment)

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 guardrex mentioned this issue Sep 4, 2018
32 tasks
@scottaddie
Copy link
Member

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

@guardrex
Copy link
Collaborator

guardrex commented Sep 6, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

No branches or pull requests

4 participants