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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:22:27+00:00 2026-06-10T22:22:27+00:00

@Html.DropDownListFor(m => m.branch, CommonMethod.getBranch(,Model.branch), –Select–, new { @multiple = multiple }) @Html.DropDownListFor(m => m.division,

  • 0
@Html.DropDownListFor(m => m.branch, CommonMethod.getBranch("",Model.branch), "--Select--", new { @multiple = "multiple" })

@Html.DropDownListFor(m => m.division, CommonMethod.getDivision(Model.branch,Model.division), "--Select--", new { @multiple = "multiple" })

I have two instances of DropDownListFor. I want to set selected as true for those which have previously stored values for Model.branch and Model.division. These are string arrays of stored ids

class CommonMethod
{
    public static List<SelectListItem> getDivision(string [] branchid , string [] selected)
    {
        DBEntities db = new DBEntities();
        List<SelectListItem> division = new List<SelectListItem>();
        foreach (var b in branchid)
            {
                var bid = Convert.ToByte(b);
                var div = (from d in db.Divisions where d.BranchID == bid select d).ToList();
                foreach (var d in div)
                {
                    division.Add(new SelectListItem { Selected = selected.Contains(d.DivisionID.ToString()), Text = d.Description, Value = d.DivisionID.ToString() });
                }
            }
        }

        return division;
    }
}

The returned value of division is selected as true for the selected item in the model, but on view side it is not selected.

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

    Use a ListBoxFor instead of DropDownListFor:

    @Html.ListBoxFor(m => m.branch, CommonMethod.getBranch("", Model.branch), "--Select--")
    
    @Html.ListBoxFor(m => m.division, CommonMethod.getDivision(Model.branch, Model.division), "--Select--")
    

    The branch and division properties must obviously be collections that will contain the selected values.

    And a full example of the proper way to build a multiple select dropdown using a view model:

    public class MyViewModel
    {
        public int[] SelectedValues { get; set; }
        public IEnumerable<SelectListItem> Values { get; set; }
    }
    

    that would be populated in the controller:

    public ActionResult Index()
    {
        var model = new MyViewModel();
    
        // preselect items with values 2 and 4
        model.SelectedValues = new[] { 2, 4 };
    
        // the list of available values
        model.Values = new[]
        {
            new SelectListItem { Value = "1", Text = "item 1" },
            new SelectListItem { Value = "2", Text = "item 2" },
            new SelectListItem { Value = "3", Text = "item 3" },
            new SelectListItem { Value = "4", Text = "item 4" },
        };
    
        return View(model);
    }
    

    and in the view:

    @model MyViewModel
    ...
    @Html.ListBoxFor(x => x.SelectedValues, Model.Values)
    

    It is the HTML helper that will automatically preselect the items whose values match those of the SelectedValues property.

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

Sidebar

Related Questions

I have multiple dropdown list: @Html.DropDownListFor(x => x.HaveColoSpace.SelectedOptions, new SelectList(Model.HaveColoSpace.Options, Value, Text), new {
I have this my view @Html.DropDownListFor(model => model.StateCode, Model.StateList, Select, new { @style =
I have this code: @this.Html.DropDownListFor(vm => vm.FishId, new SelectList(this.Model.Fishies, FishId, FishName, this.Model.FishId), Please Select
I have a select: <%: Html.DropDownListFor(c => c.DataTextField, Model, Please select, new { id=selected-contract})
I have a DropDownList: <%: Html.DropDownListFor( c => c.DataTextField, Model, Please Select Contract, new
I have the following dropdownlist in mvc <%= Html.DropDownListFor(c => c.Address.Id, new SelectList(Model.Addresses, id,
@Html.DropDownListFor(model => model.ZipFile, new SelectList(ViewBag.ZipFiles)) The above code creates me a select list just
I have the following in my View: @Html.DropDownListFor(model => model.Search, new SelectList(Model.SearchOptions)) In my
i have a dropdownlist: <%= Html.DropDownListFor(model => model.OperatingSystem, Model.OperatingSystemDropDownBowValues, new { style = width:142px;
I have this @Html.DropDownListFor(x => x.Names, new SelectList(Model.Names, Value, Text), ---Filter By Name---, new

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.