Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7990957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:09:20+00:00 2026-06-04T13:09:20+00:00

I have a view that contains many partial views and I need to pass

  • 0

I have a view that contains many partial views and I need to pass to each one the matching model.

I’ve found 2 ways to do this, but I don’t know whats the real way it should be done.

  1. I thought of creating big class that contain all the models as properties and than i can send the models to each partial view. the problem is that its hard typed, and if i need to pass a diffrent combination of models it wont fit.

  2. The other way I though of is having a methods in each model that bring me the model for each partial view (GetMenuBar() and so on).

Whats the right way doing that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:09:21+00:00Added an answer on June 4, 2026 at 1:09 pm

    My advice, go with Option 1. I use that with all of my Main View/Multiple Partial View scenarios. It’s easy to maintain as each partial has it’s own ViewModel. It keeps the whole thing nice and clean

    I use the exact same setup like so:

    public class MainViewModel {
    
        public Partial1ViewModel Partial1 [ get; set; }
        public Partial2ViewModel Partial2 [ get; set; }
        public Partial3ViewModel Partial3 { get; set; }
        public Partial4ViewModel Partial4 { get; set; }
    
        public MainViewModel() {}
    
        public MainViewModel() {
            Partial1 = new Partial1ViewModel();
            Partial2 = new Partial2ViewModel();
            Partial3 = new Partial3ViewModel();
            Partial4 = new Partial4ViewModel();
        }
    }
    

    Each PartialViewXViewModel is it’s own ViewModel and if need be can be reused in another view.

    Your Action that renders can look like so:

    public ActionResult Index {
        var model = new MainViewModel();
        return View(model);
    }
    

    Your View

    @model MainViewModel
    
    <div>
        {@Html.RenderPartial("PartialOne", Model.Partial1)}
    </div>
    
    
    <div>
        {@Html.RenderPartial("PartialTwo", Model.Partial2)}
    </div>
    
    
    <div>
        {@Html.RenderPartial("PartialThree", Model.Partial3)}
    </div>
    
    
    <div>
        {@Html.RenderPartial("PartialFour", Model.Partial4)}
    </div>
    

    Define the UI for each PartialX like:

    @model Partial1ViewModel
    
    //view html here
    

    Now, each Partial view html and each model that they use can be used anywhere.

    The great part is now if you have a page that needs only 2 of these you just create a new ViewModel to represent that specific view like so:

    public class OtherMainViewModel {
    
        public Partial2ViewModel Partial2 [ get; set; }
        public Partial4ViewModel Partial4 { get; set; }
    
        public OtherMainViewModel() {}
    
        public OtherMainViewModel() {
            Partial2 = new Partial2ViewModel();
            Partial4 = new Partial4ViewModel();
        }
    }
    

    And use it in another view like so:

    public ActionResult SomeOtherAction {
        var model = new OtherMainViewModel();
        return View(model);
    }
    

    And that’s perfectly acceptable and also the preferred design strategy in MVC, to have ViewModels that specifically represent what a view needs and only what it needs.

    You may want to use a different method for populating your models tho. Most here would recommend using Automapper. Either way, the above just initializes the PartialViewXModels in the constructor of the MainViewModel. That won’t necessarily be your case if you are populating those models with data from your DB. You would want your own strategy for that. This would work here:

    public ActionResult Index {
        var model = new MainViewModel();
        model.Partial1 = GetPartial1Data(); // this method would return Partial1ViewModel instance
        model.Partial2 = GetPartial2Data(); // same as above for Partial2ViewModel
        ...
        return View(model);
    }
    

    This all would just get you started with the design, you can tweak it to your hearts content 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an edit object view that contains a formset(one or many if this
I have a view model that contains a Product class type and an IEnumerable<
I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured
I have a view that contains a javascript function that i need to init/call
I have a table that contains my node data in a tree view. Each
I have a view that contains two NSTextFieldCell s. The size at which these
I'm developing an iOS 4 application. I have a main view that contains another
I have a hypothetical tree view that contains this data: RootNode Leaf vein SecondRoot
I have developed a small view that contains special char like 'é' and when
I currently have a product view page that contains an MVCContrib HTML Grid with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.