Razor Page Code like below (reference to: https://www.learnrazorpages.com/razor-pages/ajax/partial-update)
```c#
public class AjaxPartialModel : PageModel
{
private ICarService _carService;
public AjaxPartialModel(ICarService carService)
{
_carService = carService;
}
public List
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:
```c#
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:
c#
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
Steps to reproduce the behavior:
I am also running into this one on 2.2
I am facing the same problem using VS 2017 and Core 2.2.
As I understand it is fixed, so please let me know what should I do to use Partial Views with Razor pages. Currently, my project is suspended.
Can you suggest any turnover solution before it will be fixed in the new Core version?
@devtalex The following works for me in 2.2
public async Task<IActionResult> OnPostAsync()
{
var result = await someAsyncStuff();
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary()) {Model = result};
return new PartialViewResult {ViewName = "_YourPartialViewName", ViewData = viewData};
}
@devtalex The following works for me in 2.2
public async Task<IActionResult> OnPostAsync() { var result = await someAsyncStuff(); var modelMetadataProvider = new EmptyModelMetadataProvider(); var viewData = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary()) {Model = result}; return new PartialViewResult {ViewName = "_YourPartialViewName", ViewData = viewData}; }
Thank you for feedback, but it still does not work - the problem is that the whole main page (main Index page) is loaded into designated DIV tag, instead of Partial View page content.
See the details below, maybe you will find something wrong.
Razor Index.cshtml
`
$(function () {
$('#testajaxcall').click(function () {
console.log("testajaxcal = TEST");
///ajax call
$.ajax({
url: '/Payments?handler=TestAjaxCal',
type: 'GET',
cache: false,
}).done(function (result) {
$('#containera').html(result);
});
////
}); });
_Partial2.cshtml
@model List
List of Departments
`
Name Handler in Razor Model file:
`
public PartialViewResult OnGetTestAjaxCall()
{
var result = new List
long depid = 5;
result = _context.Department.Where(o =>
o.DepartmentID == depid
).ToList();
var modelMetadataProvider = new EmptyModelMetadataProvider();
var viewData = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary()) { Model = result };
return new PartialViewResult { ViewName = "_Partial2", ViewData = viewData };
}
`
Seems you have a spelling error. In the AJAX call, you are calling the handler TestAjaxCal. But your handler in the C# code is named OnGetTestAjaxCall - in other words one l vs 2 ls
Fix the handler in the AJAX to be named TestAjaxCall
In 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.
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.
Thank you, Jerrie, now it works!