sur.la.route
sur.la.route

Reputation: 550

Partial views no longer get View Data after upgrading from .NET Core 2.2 to .NET 5

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:

enter image description here

Is it a bug?

Upvotes: 0

Views: 228

Answers (1)

Brando Zhang
Brando Zhang

Reputation: 28267

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

Upvotes: 1

Related Questions