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 8791263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:49:07+00:00 2026-06-13T22:49:07+00:00

Let’s say I want to show multiple Drop Down Boxes each with the same

  • 0

Let’s say I want to show multiple Drop Down Boxes each with the same values within them within a View using the @Html.DropDownList.
Is there an easy way to be able to render all of these Drop Down Boxes without having to bind something like the ViewBag to each of the months returned?

For example:

Controller

List<SelectListItem> items = new List<SelectListItem>();

var values = (from v in db.Values select v);

foreach (var value in values)
{
    items.Add(new SelectListItem { Text = value.Name, Value = value.Id });
}

ViewBag.Values = PopulateSelectList("Values");

View

@Html.DropDownList("Values")

But what the goal would be is to have something like

<div class="control-group">
    @Html.LabelFor(model => model.DesignStyle)
    <div class="controls">
        @Html.DropDownList("Values")    @Html.DropDownList("Values")
        @Html.DropDownList("Values")    @Html.DropDownList("Values")
        @Html.DropDownList("Values")    @Html.DropDownList("Values")
        @Html.DropDownList("Values")    @Html.DropDownList("Values")
        @Html.DropDownList("Values")    @Html.DropDownList("Values")
    </div>
</div>

My guess is I would need some sort of @foreach (item in Model) to populate each of the DropDowns on the View

Here is a sample of my initial thought on how to get something going.

List<SelectListItem> results = new List<SelectListItem>();
SelectListItem item = null;

var values = (from v in db.Values select v);

for (int i = 0; i <= 15; i++)
{
     item = new SelectListItem();

     item.Text = "Select a value";
     item.Value = "1";

     results.Add(item);

     foreach (var thread in threads)
     {
         item.Text = thread.Name;
         item.Value = thread.Id;

         results.Add(item);
     }
 }

What I don’t want to do is duplicate the logic to populate a SelectListItem multiple times and put each of those in a separate ViewBag item.

  • 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-13T22:49:08+00:00Added an answer on June 13, 2026 at 10:49 pm

    I ended up using the EditorTemplate functionality in order to solve / render this.

    The Model changed from this:

    public class ValueModel
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    }
    

    To this:

    public class ValueModel
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
    
        public List<ValueDropDownModel> DropDown { get; set; }
    }
    
    public class ValueDropDownModel
    {
        [Range(0, 15)]
        public int DropDown { get; set; }
    
        public int Id { get; set; }
        public string Name { get; set; }
    
        public List<SelectListItem> AvailableDropDown { get; set; }
    }
    

    Within the Views under the folder for Value, there is now a sub folder called EditorTemplates. This contains 2 Views. A ValueModel.cshtml and a ValueDropDownModel.cshtml.

    ValueModel.cshtml contains:

    @model SomeSolution.Web.Models.ValueModel
    
    <div class="row-fluid">
    
        <div class="span6">
            <div class="control-group">
                @Html.LabelFor(m => m.Name)
                <div class="controls">
                    @Html.EditorFor(m => m.Name)
                    @Html.ValidationMessageFor(m => m.Name)
                </div>
            </div>
    
        </div>
    </div>
    
    <div class="row-fluid">
        <div class="span4">
            @for (int i = 0; i < 5; ++i)
            {
                @Html.EditorFor(m => m.DropDown[i])
            }
        </div>
    
        <div class="span4">
            @for (int i = 5; i < 10; ++i)
            {
                @Html.EditorFor(m => m.DropDown[i])
            }
        </div>
    
        <div class="span4">
            @for (int i = 10; i < 15; ++i)
            {
                @Html.EditorFor(m => m.DropDown[i])
            }
        </div>
    </div>
    

    ValueDropDownModel.cshtml contains:

    @model SomeSolution.Web.Models.ValueDropDownModel
    
    <div class="control-group">
        @Html.HiddenFor(m => m.DropDown)
        @String.Format("DropDown {0}", Model.DropDown)
        <div class="controls">
            @Html.DropDownListFor(m => m.Id, Model.AvailableDropDown)
            @Html.ValidationMessageFor(m => m.Id)
        </div>
    </div>
    

    The ValueController now includes some helper methods to populate the drop downs

        private void FillAvailableValues(ValueModel modelValue, Entities db)
        {
            var values = (from v in db.Values
                           orderby v.Name
                           select v);
    
            foreach (var model in modelValue.Values)
            {
                model.AvailableDropDown = new List<SelectListItem>();
                model.AvailableDropDown.Add(new SelectListItem()
                {
                    Text = "Unassigned",
                    Value = "0",
                    Selected = (model.Id == 0)
                });
    
                foreach (var value in values)
                {
                    model.AvailableDropDown.Add(new SelectListItem()
                    {
                        Text = value.Name,
                        Value = value.Id,
                        Selected = (model.Id.ToString() == colour.Id)
                    });
                }
            }
        }
    
        private void InitDefaultDropDown(ValueModel model)
        {
            model.DropDown = new List<ValueDropDownModel>();
            for (int i = 0; i < 15; i++)
            {
                model.DropDown.Add(new ValueDropDownModel()
                {
                    DropDown = i + 1,
                    Id = 0
                });
            }
        }
    

    The FillAvailableValues method is called on the Create ActionResult as well as the Edit ActionResult to initialize the Drop Downs. The InitDefaultDropDown method is called on the Create ActionResult to setup the Drop Downs on the page.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have thousands of users and I want to make the passwords
Let say I want to create an iOS app that download music files from
Let's say I have multiple requirements for a password. The first is that the
Let say I've this URL: http://example.com/image-title/987654/ I want to insert download to the part
Let's say I want to write an FTP client from scratch. In the command
Let's say I have a drive such as C:\ , and I want to
Let's say I have an Instant Messenger server using SignalR. I want to broadcast
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
Let say I want to test my controller behavior, but it accepts JSON string

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.