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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:21:36+00:00 2026-06-05T21:21:36+00:00

In a ASP.NET MVC (Razor) project, I’m using a ListBox with Multi Select option

  • 0

In a ASP.NET MVC (Razor) project, I’m using a ListBox with Multi Select option in a Edit View,

CONTROLLER

    public ActionResult Edit(int id)
    {
        Post post = db.Posts.Find(id);
        string selectedValues = post.Tags; //This contains Selected values list (Eg: "AA,BB")
        ViewBag.Tagslist = GetTags(selectedValues.Split(','));
        return View(post);
    }

    private MultiSelectList GetTags(string[] selectedValues)
    {
        var tagsQuery = from d in db.Tags
                        orderby d.Name
                        select d;
        return new MultiSelectList(tagsQuery, "Name", "Name", selectedValues);

    }

HTML

    <div class="editor-field">
        @Html.ListBox("Tags", ViewBag.Tagslist as MultiSelectList)
    </div>

This loads the items (Tag List) in to ListBox, but does not highlight the items in the Selected Values list.

How to fix this issue?

Thanks in advance.

  • 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-05T21:21:37+00:00Added an answer on June 5, 2026 at 9:21 pm

    I suspect that your Post class (to which your view is strongly typed) has a property called Tags. You also use Tags as first argument of the ListBox helper. This means that the helper will look into this property first and ignore the selected values you passed to the MultiSelectList. So the correct way to set a selected value is the following:

    public ActionResult Edit(int id)
    {
        Post post = db.Posts.Find(id);
        ViewBag.Tagslist = GetTags();
        return View(post);
    }
    
    private MultiSelectList GetTags()
    {
        var tagsQuery = from d in db.Tags
                        orderby d.Name
                        select d;
        return new MultiSelectList(tagsQuery, "Name", "Name");
    
    }
    

    and in the view:

    <div class="editor-field">
        @Html.ListBoxFor(x => x.Tags, ViewBag.Tagslist as MultiSelectList)
    </div>
    

    And here’s a full example that should illustrate:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var post = new Post
            {
                Tags = new[] { "AA", "BB" }
            };
            var allTags = new[]
            {
                new { Name = "AA" }, new { Name = "BB" }, new { Name = "CC" },
            };
            ViewBag.Tagslist = new MultiSelectList(allTags, "Name", "Name");
            return View(post);
        }
    }
    

    Also I would recommend you using view models instead of passing your domain entities to the view. So in your PostViewModel you will have a property called AllTags of type MultiSelectList. This way you will be able to get rid of the weak typed ViewBag.

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

Sidebar

Related Questions

The DataAnnotations validator not working in asp.net mvc 4 razor view, when using the
I have an Asp.Net mvc 3 project I'm using razor, and need to generate
I am using Visual Studio 2010 / ASP.net MVC 3 with the Razor View
I am programming using ASP.NET MVC 3 with Razor view engine. Here's what I'd
When I try to add a partial view using asp.net mvc 3 and razor
I'm using Chosen JQuery Plugin for a ASP.NET MVC (Razor) Project. For a Dropdownlist
For my website project I am using ASP.NET MVC Razor. Learning as I go.
I have created a sample project using ASP.NET MVC 3 Web Application (Razor) template.
Using the new ASP.NET MVC 3.0 Razor View Engine, is there any way to
I am using ASP.Net MVC 3 Razor view engine. I have a requirement to

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.