question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Partial(string viewName, object model) not working when use in Razor Page

See original GitHub issue

Describe 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:

  1. Using this version of ASP.NET Core 2.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:6

github_iconTop GitHub Comments

1reaction
devtalexcommented, Oct 14, 2019

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.

Thank you, Jerrie, now it works!

1reaction
jerriepcommented, Oct 14, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found