Search code examples
c#asp.netasp.net-core.net-corerazor-pages

How to pass a model to a partial view in _Layout.cshtml page in razor pages


I am currently developing a web app in Razor pages and have hit a bump.

The problem is that I have a bootstrap Modal form for registration on every page. So I decided to have it as a partial view and include it in my _Layout page so that it appears on every page. But the issue is how do I pass model (here:RegisterModel) to it. After a little research I found the following solution.

Make a base class (here: MyPageModel.cs )which inherits PageModel and contains a public property referencing the model I want. Then have this class inherited in all my pages in place of PageModel. But the problem is that, now I'm bound with RegisterModel on every razor page.

 public class MyPageModel : PageModel
    {
        [BindProperty]
        public RegisterModel RegisterModel { get; set; }

        public async Task<IActionResult> OnPostRegisterAsync()
        {

            if (ModelState.IsValid)
            {

                RegisterModel emailData = new()
                {
                    FirstName = RegisterModel.FirstName,
                    LastName = RegisterModel.LastName,
                    Mobile = RegisterModel.Mobile,
                    Email = RegisterModel.Email,
                    PreferredStream = RegisterModel.PreferredStream,
                    ProgramType = RegisterModel.ProgramType,
                    Board = RegisterModel.Board,
                    DistantLearning = RegisterModel.DistantLearning
                };

                int response = await SendFluentEmail.Send(emailData);

                return Content(response.ToString());
            }
            else
            {
                return Page();
            }

        }
    }

For instance, I have a contact us page where I'm using a different model. But when the post method for this page is called the model state returns invalid because of the previous bound with RegisterModel.

public class contact_usModel : MyPageModel
    {
        [BindProperty]
        public ContactModel contact { get; set; }


        public IActionResult OnGet()
        {
            return Page();
        }

      
        public async Task<IActionResult> OnPostContactAsync()
        {
            
            if (ModelState.IsValid)
            { 

                ContactModel sendData = new()
                {
                    FirstName = contact.FirstName,
                    LastName = contact.LastName,
                    Email = contact.Email,
                    Subject = contact.Subject,
                    Message = contact.Message
                };

                int response  = await SendFluentEmail.Send(sendData);

                return Content(response.ToString());
            }
            else
            {
                return Page();
            }

        }
    }

This is how I'm passing the model to partial view in _Layout page.

<partial name="_RegisterModalView" model="Model.RegisterModel" />

This is my ContactModel

public class ContactModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Subject { get; set; }
        public string Message { get; set; }
    }

This is my RegisterModel

public class ContactModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Mobile { get; set; }
        public string PreferredStream { get; set; }
        .
        .
        .
    }

So how do I solve this issue? Please Help.


Solution

  • If you want to validate ContactModel,you can try to use TryValidateModel:

     if (TryValidateModel(contact))
        {
            ...
        }
    

    If it still does not work,try this:

    ModelState.Clear();
    if (TryValidateModel(contact))
    {
        ...
    }