Home > Enterprise >  How to call multiple partial views with their View Models inside a view - ASP.NET MVC
How to call multiple partial views with their View Models inside a view - ASP.NET MVC

Time:01-11

I have a view called Register where I call other views using @Html.Partial("ViewName") I wanted to know how I can call other ViewModels from the partial views inside the view.

I know that each of the View Models with their fields can be declared at the top, but I wanted to know if there is something better that can be done?

View

@model ViewModels.Account.RegisterVM 


<div  id="acdRegistration">
    <div >
        <h2  id="headClient">
            <button  type="button" data-bs-toggle="collapse" data-bs-target="#colClient" aria-expanded="true" aria-controls="colClient">
                Client Registration
            </button>
        </h2>
        <div id="colClient"  aria-labelledby="headClient" data-bs-parent="#acdRegistration">
            <div >
                @Html.Partial("_RegisterClient")
--> Here I should call ClientRegistrationVM 
            </div>
        </div>
    </div>
    <div >
        <h2  id="headClinic">
            <button  type="button" data-bs-toggle="collapse" data-bs-target="#colClinic" aria-expanded="false" aria-controls="colClinic">
                Clinic Registration
            </button>
        </h2>
        <div id="colClinic"  aria-labelledby="headClinic" data-bs-parent="#acdRegistration">
            <div >
                @Html.Partial("_RegisterClinic")
--> Here I should call ClinicRegistrationVM 


            </div>
        </div>
    </div>
    <div >
        <h2  id="headRdvm">
            <button  type="button" data-bs-toggle="collapse" data-bs-target="#colRdvm" aria-expanded="false" aria-controls="colRdvm">
                RDVM Registration
            </button>
        </h2>
        <div id="colRdvm"  aria-labelledby="headRdvm" data-bs-parent="#acdRegistration">
            <div >
                @Html.Partial("_RegisterUser")
--> Here I should call UserRegistrationVM 

            </div>
        </div>
    </div>
</div>

ViewModels

 public class RegisterVM 
    {

        public List<string> RegistrationTypes
        {
            get { return new List<string>() { "Client", "Clinic", "RDVM" }; }
        }

        [Required(ErrorMessage = "Email address required")]
        public string EmailAddress { get; set; }

        public string LastName { get; set; }

        public string FirstName { get; set; }

        public string PatientName { get; set; }

        public string PatientId { get; set; }

        [Required(ErrorMessage = "Registration type required")]
        public string RegistrationType { get; set; }

    }

    public class ClinicRegistrationVM 
    {
        [Required(ErrorMessage = "Email address required")]
        public string EmailAddress { get; set; }

        public string PatientName { get; set; }

        public string PatientId { get; set; }

    }
    public class ClientRegistrationVM 
    {
        [Required(ErrorMessage = "Email address required")]
        public string EmailAddress { get; set; }

        [Required(ErrorMessage = "Last name required")]
        public string LastName { get; set; }

        [Required(ErrorMessage = "First name required")]
        public string FirstName { get; set; }

        public string PatientName { get; set; }

        public string PatientId { get; set; }

    }


    public class UserRegistrationVM 
    {
        [Required(ErrorMessage = "Email address required")]
        public string EmailAddress { get; set; }

        public string PatientName { get; set; }

        public string PatientId { get; set; }

    }

CodePudding user response:

There's an overloaded version of HtmlHelper.Partial that receives a model object that will be passed to the partial view.

Html.Partial("PartialViewName", ModelObject)
  • Related