how use partial with model in _layout page asp.net core Razor pages

Ashkan 81 Reputation points
2024-08-17T10:11:29.72+00:00

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:

Screenshot 2024-08-17 132117

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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,542 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,879 questions
0 comments No comments
{count} votes

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.