You must Sign In to post a response.
  • Category: ASP.Net MVC

    Ideal model strurcture for an mvc project for handeling multiple partial views

    I have two partial views named as _centerDetails.cshtml and _centerRights.cshtml .

    I am passing data from centerdetails when i click on submit and want to show this when other partial view is switched but without posting it to server.

    Question Update

    I found that my question was not well explained.

    This is my model class which i have created to handling my data via controler.

    namespace ADWP_AdminWebPortal.Models {

    public class CountryList
    {
    [Required(ErrorMessage = "Select a Country.")]
    public int CountryId { get; set; }
    [Required]
    public string Country { get; set; }

    [Required(ErrorMessage = "Select a State.")]
    public int StateId { get; set; }
    [Required]
    public string State { get; set; }

    [Required(ErrorMessage = "Select a City.")]
    public int CityId { get; set; }
    [Required]
    public string City { get; set; }

    }

    public class CustomerDetails: CountryList
    {

    public int ClientId { get; set; }

    [Required (ErrorMessage="Eneter the First name")]
    [DataType(DataType.Text)]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Eneter the Middle name")]
    [DataType(DataType.Text)]
    public string MiddleName { get; set; }

    [Required(ErrorMessage="Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string MobileNo { get; set; }

    [Required(ErrorMessage = "Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string MobileNo2 { get; set; }


    [Required(ErrorMessage = "Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string LandLine { get; set; }


    [Required(ErrorMessage = "Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string LandLine2 { get; set; }

    [Required(ErrorMessage = "Eneter the Last name")]
    [DataType(DataType.Text)]
    public string LastName { get; set; }

    public string NickName { get; set; }

    [Required(ErrorMessage = "Enter The Address")]
    [MaxLength(300)]
    public string Address { get; set; }

    [Required (ErrorMessage ="Enter the Zipcode.")]
    [DataType(DataType.PostalCode)]
    [Range(4,6)]
    public string ZipCode { get; set; }

    [Required(ErrorMessage = "Please Enter Email Id")]
    [DataType(DataType.EmailAddress)]
    [MaxLength(50)]
    [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
    public string EmailId { get; set; }

    [Required(ErrorMessage = "Please Enter Email Id")]
    [DataType(DataType.EmailAddress)]
    [MaxLength(50)]
    [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]

    public string ConfirmEmailId { get; set; }

    public string RefferalId { get; set; }

    [Required(ErrorMessage="Select Any Occupation.")]
    public string Occupatioin { get; set; }

    public string NatureOfOccupation { get; set; }

    public string AgentId { get; set; }

    [Required(ErrorMessage ="Select a Client Type.")]
    public string ClientType { get; set; }


    public int TariffId { get; set; }
    public string TariffName { get; set; }
    public int ServiceId { get; set; }
    public string ServiceName { get; set; }
    public string OrderId { get; set; }
    public int PaymentMethodId { get; set; }
    public string PaymentMethodName { get; set; }

    }
    }

    Here you can see the mess i have created. My problem is how to handle multiple models when you are working with multiple models in same controler?

    Here i did not created all the data in a single class because i wanted it to reuse as per my need.

    Have you guys any idea about handling data in a model this is the main problem that is coming to me while i am creating the partial views in my project.
  • #769337

    Hi Rajan Mishra.

    Handling multiple models (clasess) in the same controller is possible using viewmodel concept.

    Have a look on below code once to gt some idea.


    public class CountryList
    {
    [Required(ErrorMessage = "Select a Country.")]
    public int CountryId { get; set; }
    [Required]
    public string Country { get; set; }

    [Required(ErrorMessage = "Select a State.")]
    public int StateId { get; set; }
    [Required]
    public string State { get; set; }

    [Required(ErrorMessage = "Select a City.")]
    public int CityId { get; set; }
    [Required]
    public string City { get; set; }

    }

    public class CustomerDetails
    {

    public int ClientId { get; set; }

    [Required (ErrorMessage="Eneter the First name")]
    [DataType(DataType.Text)]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Eneter the Middle name")]
    [DataType(DataType.Text)]
    public string MiddleName { get; set; }

    [Required(ErrorMessage="Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string MobileNo { get; set; }

    [Required(ErrorMessage = "Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string MobileNo2 { get; set; }


    [Required(ErrorMessage = "Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string LandLine { get; set; }


    [Required(ErrorMessage = "Please Enter Valid Mobile No")]
    [DataType(DataType.PhoneNumber)]
    public string LandLine2 { get; set; }

    [Required(ErrorMessage = "Eneter the Last name")]
    [DataType(DataType.Text)]
    public string LastName { get; set; }
    }

    public class viewmodel
    {
    public IEnumerable<CustomerDetails> _cDetails { get; set; }
    public IEnumerable<CountryList> _cList { get; set; }
    }


    From the above code when you pass viewmodel to view, then you can use _cDetails or _cList as per the requirement.
    For more idea I recommend you to go through what is viewmodel? why we need view model?


    Sridhar Thota.
    Editor: DNS Forum.


  • Sign In to post your comments