View components solve this problem. See the official documentation
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-8.0
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am programming my project with asp.net core Razor Page and In the upper part of , I have allocated a space for the user and her messages, which I have used to call partial in the layout file:
<div class="header-left">
<partial name="_HeaderTools" model="Model.AuthUser" />
</div>
this picture:
These are the codes of the index file:
public class IndexModel : PageModel
{
[BindProperty]
public UserViewModel AuthUser { get; set; }
private DbkabirContext db;
public IndexModel(DbkabirContext context)
{
db = context;
}
public void OnGet()
{
var user = db.Users.Where(x => x.NatNo == User.Identity.Name).FirstOrDefault();
AuthUser = new UserViewModel()
{
Id = user.Id,
Title = user.LastNameFa
};
}
}
Now, as soon as it is executed, everything goes well and the _HeaderTools Partial is correctly called and executed, and the required information is displayed.
But the problem is that when I run one of the sidebar menu items and naturally _layout is called along with partial, I get an error this time. Of course, the error is quite clear that it needs to fill the model related to a partial, and this model is not filled in other sidebar items and is only filled once in the primary index.
An unhandled exception occurred while processing the request.
RuntimeBinderException: 'KabirLang.Areas.Staff.Pages.BasicInfo.TermListModel' does not contain a definition for 'AuthUser'
Please advise what is the solution that I should use?
thank you
View components solve this problem. See the official documentation
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-8.0