Partial(string viewName, object model) not working when use in Razor Page
See original GitHub issueDescribe the bug
Razor Page Code like below (reference to: https://www.learnrazorpages.com/razor-pages/ajax/partial-update)
public class AjaxPartialModel : PageModel
{
private ICarService _carService;
public AjaxPartialModel(ICarService carService)
{
_carService = carService;
}
public List<Car> Cars { get; set; }
public void OnGet()
{
}
public PartialViewResult OnGetCarPartial()
{
Cars = _carService.GetAll();
return Partial("_CarPartial", Cars);
}
}
will not work. method Partial will throw an exception like:
InvalidOperationException: The model item passed into the ViewDataDictionary is of type ‘List< Car >’, but this ViewDictionary instance requires a model item of type 'AjaxPartialModel '.
so I checked the Partial’s source code, found:
public virtual PartialViewResult Partial(string viewName, object model)
{
ViewContext.ViewData.Model = model;
// here ViewContext is member of AjaxPartialModel, so it accept only model of type AjaxPartialModel,
// so I think here we need create a new ViewDataDictionary to accept model in parameter.
return new PartialViewResult
{
ViewName = viewName,
ViewData = ViewContext.ViewData
};
}
maybe this method should change to:
public virtual PartialViewResult Partial<T>(string viewName, T model)
{
return new PartialViewResult
{
ViewName = viewName,
ViewData = new ViewDataDictionary<T>(ViewData, model);
};
}
I had not check this implementation in depth, so maybe something is wrong above. Appreciate further details
To Reproduce
Steps to reproduce the behavior:
- Using this version of ASP.NET Core 2.2
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:6
Top Results From Across the Web
c# - Render Razor View To string not working with multiple ...
Found a solution, everything worked after replacing Html.PartialAsync with Html.RenderAsync in the foreach loop.
Read more >Partial views in ASP.NET Core
Declare partial views In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and ...
Read more >Partial Tag Helper in ASP.NET Core
The Partial Tag Helper is used for rendering a partial view in Razor Pages and MVC apps. Consider that it: Requires ASP.
Read more >Back to Basics: Rendering Razor Views to String in ASP.NET ...
The rendering method takes a view path, model and a controller context to retrieve the view using the active RazorViewEngine of the application....
Read more >Partial Page Update with AJAX in Razor Pages
A partial can be strongly typed - have an @model directive, or it can work purely with ViewData . The following examples are...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Thank you, Jerrie, now it works!
Seems you have a spelling error. In the AJAX call, you are calling the handler
TestAjaxCal. But your handler in the C# code is namedOnGetTestAjaxCall- in other words onelvs 2lsFix the handler in the AJAX to be named
TestAjaxCallIn either case, your problem is not related to this particular GitHub issue. If my suggestion above does not work for you, then I suggest you rather seek help at a place like StackOverflow.