0

I'm working on updating a web app from .NET Core 2.2 to .NET 5. Most things are working pretty well, but I'm stuck on the partial views.

The website uses a ton of Ajax requests and most return a small partial view with some HTML. All the variable information in partial was set through the view data.

After updating to .NET 5 the viewdata comes into the partial view as empty.

For example, I'm returning a partial "_mailbox". In the controller you can see that I have stuff in the view data:

enter image description here

but when I step into the partial view you can see it is blank! In 2.2 it was passed through:

enter image description here

The basic function:

public ActionResult OnGetGetMailbox(int id)
{
  ViewData["Fullname"] = "Christopher"
  return Partial("_mailbox")
}

Do you have any ideas what may have gone wrong? I've basically just followed the steps outlined by Microsoft.

How to Reproduce:

I started a brand new project to test and have the same issue -

enter image description here enter image description here

Add a new HTML partial _test.cshtml

enter image description here

Add a new function in index.cshtml.cs

enter image description here

Then when running the web app https://localhost:44332/?Handler=test

I should see my name:
enter image description here

But it is blank. When debugging and stepping through I see the view data is not passed to the view.

Other information:

  • It works in .NET Core 2.2.105.

  • Per the Microsoft docs the same syntax as 2.2 should work in 5.1. However, as Brando Zhang pointed out, you have to use the antiquated syntax from .NET 2.1 to make this work.

  • If you read the docs for the Partial function for .NET 5 it claims to be part of the Microsoft.AspNetCore.Mvc.RazorPages namespace, but if you try to use that namespace you will find Partial does not exist. see docs

enter image description here

Is it a bug?

3
  • According to your description, I have created a test demo on my side, it works well. I could get the result. Image. Have you tired to create a new .net 5 project and do some test? Commented Feb 24, 2021 at 9:34
  • @BrandoZhang thanks for taking the time to do a demo. I created a separate project as well, but didn't pass the partials. Here are links to my startup, project, and .csproj, I wonder how they differed from your? They match closely (except the extra packages) what the default is now! csproj startup.cs project.cs Commented Feb 24, 2021 at 14:01
  • @BrandoZhang I added steps to reproduce with a fresh demo. Does it do the same for you? Thanks! Commented Feb 24, 2021 at 16:43

1 Answer 1

1

As far as I know, the return Partial is MVC function result not razor pages, if you want to use razor page's Partial result, you should build by yourself.

More details, you could refer to below codes:

public IActionResult OnGetTest()
{

    ViewData["Test"] = "test";
    var partialView = "_test";


    var partialViewResult = new PartialViewResult()
    {
        ViewName = partialView,
        ViewData = ViewData
    };
    return partialViewResult;
}   

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

but if I follow the exact steps to reproduce using the DotNet core 2.2 sdk instead of 3.1 or 5.0 it works :(
but you are right as well, I suppose this is the only way to make it work in 3.1 and 5. Another win for microsoft lol
from the docs it seems that there should have been not change from 2.2 to 3.1 or 5 and the syntax you found to work is from dotnet <= 2.1?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.