I have a ASP.NET Core Web Application, using RazorPages. I'm using a partial view for searching for a person. This partial view model (base PageModel) has a constructor with an interface to supply a repository (using DI). This partial view model also has a field for binding the selected persons id.
I use this partial view in a RazorPage, where I want to know the selected persons id. So this RazorPage has a PageModel, with a property being the PageModel for the partial view.
Like this:
public class _PartialPageModel : PageModel
{
private readonly IRepository repository;
[BindProperty]
public int SelectedPersonId {get; set;}
public List<SelectListItem> SearchValues {get;set;}
public _PartialPageModel(){}
public _PartialPageModel(IRepository repository){
this.repository = repository;
}
public void OnGet(){
//use the repository to fill the SearchValues
}
}
public class RazorPageModel : PageModel{
[BindProperty]
public _PartialPageModel SearchModel {get; set;}
private readonly IRepository repository;
public RazorPageModel(IRepository repository){
this.repository = repository;
}
public void OnGet(){
SearchModel = new _PartialPageModel(repository);
}
}
In the Razor Markup page: <partial name="_PartialPage" for="@Model.SearchModel"/>
However, this gives me the following error:
System.InvalidOperationException: Could not create an instance of type 'Microsoft.AspNetCore.Http.HttpContext'. Model bound complex types must not be abstract or value types and must have a parameterless constructor. Record types must have a single primary constructor. Alternatively, set the 'HttpContext' property to a non-null value in the 'MyProject.Pages.Shared._PartialPageModel' constructor.
I searched and tries several alternatives, but nothing that does not return an error when posting back to the RazorPageModel.