Reputation: 550
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:
but when I step into the partial view you can see it is blank! In 2.2 it was passed through:
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 -
Add a new HTML partial _test.cshtml
Add a new function in index.cshtml.cs
Then when running the web app https://localhost:44332/?Handler=test
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
Is it a bug?
Upvotes: 0
Views: 228
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:
Upvotes: 1