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

  • Home
  • SEARCH
  • 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 6056957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:26:07+00:00 2026-05-23T08:26:07+00:00

I have a partial view that contains a few <div> s that look the

  • 0

I have a partial view that contains a few <div>s that look the same and every each of them in their turn contain a DropdownList. Now, selected values in the DropdownLists usually different. And initial list of values comes from some external source.
So that would’t work:

public PartialViewResult GetMeals()
{
      var meals = DataContext.GetMeals();
      ViewBag.Units = new SelectList(DataContext.GetUnits,"Id","Name");

      return PartialView("_Meals", meals);
 }

@foreach (var m in Meals)
 {
     <div>  
         @Html.DropDownList("Units", ViewBag.Units as List<SelectListItem>)
     <dig>

I can of course create another partial view for each Meal, but I don’t wanna get into a partial views hierarchy and create Partial inside another Partial (Although Leo DiCaprio would’ve love that)

Can you guys give me an advice?

  • 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-05-23T08:26:07+00:00Added an answer on May 23, 2026 at 8:26 am

    Here’s what I would do. This first thing is to get rid of ViewBag. Then define a view model:

    public class MealViewModel
    {
        public string MealDescription { get; set; }
    
        public string SelectedUnit { get; set; }
        public IEnumerable<SelectListItem> Units { get; set; }
    }
    

    Inside this view model you put only what your view (partial view in this case) needs.

    and then I would have my controller action populate this view model by aggregating data from wherever you want:

    public ActionResult GetMeals()
    {
        var meals = DataContext.GetMeals().ToList(); // <-- being eager with .ToList()
        var units = DataContext.GetUnits().ToList(); // <-- being eager with .ToList()
        var viewModel = meals.Select(meal => new MealViewModel
        {
            MealDescription = meal.Description,
            SelectedUnit = meal.UnitId,
            Units = units.Select(unit => new SelectListItem
            {
                Value = unit.Id.ToString(),
                Text = unit.Name
            })
        });
        return PartialView("_Meals", viewModel);
    }
    

    and inside the partial _Meals I would use editor templates:

    @model IEnumerable<MealViewModel>
    @Html.EditorForModel()
    

    and finally I would define an editor template for a meal: (~/Views/Shared/EditorTemplates/MealViewModel.cshtml) which will be rendered for each element of the model:

    @model MealViewModel
    <h3>@Html.DisplayFor(x => x.MealDescription)</h3>
    <div>    
        @Html.DropDownListFor(
            x => x.SelectedUnit,
            new SelectList(Model.Units)
        )
    </div>
    

    Now more loops, casts, wrongly named input controls. You get strongly typed, Intellisense enabled views, yummy 🙂

    Now when you look at the controller action there must be something that bothers you: this repetitive mapping code between your domain models and the view models. Enter the world of AutoMapper and you will get really pretty code.

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

Sidebar

Related Questions

I have a number of pages that include the same partial view which contains
I have one (razor) page that contain 5 different partial views. Each partial view
I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured
I have a partial view that contains an img like so: <img src= alt=test
I have multiple pages containing the same partial view. The partial contains a form
I have a strong typed partial view of a class that contains a list
I have a partial view that I want to basically take care of itself
I have a partial view that is shared between two controllers and I'm trying
I have a partial view (UserControl) that implements a simple pager in my Asp.Net
I'm using MVC2 with VS2010 I have a view that has two partial views

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.