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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:50:05+00:00 2026-06-10T03:50:05+00:00

So I have the following (pseudo code): string selectedvalud = C; List<SelectListItem> list= new

  • 0

So I have the following (pseudo code):

string selectedvalud = "C";
List<SelectListItem> list= new List<SelectListItem>();
foreach(var item in mymodelinstance.Codes){
  list.Add(new SelectListItem { Text = item.Name, Value = item.Id.Tostring(), Selected = item.Id.ToString() == selectedvalue ? true : false });
}

ViewBag.ListOfCodes = list;

on my view:

<%: Html.DropDownList("Codes", (List<SelectListItem>)ViewBag.ListOfCodes , new { style = "max-width: 600px;" })%>

now, before it reaches the view, the “list” has populated it with items and has marked the item which is already selected. but when it gets to the view, none of the options are marked as selected.

my question is, is it possible to use a viewbag to pass the items or should i use a different medium? as it removes the selected flag on the options if i use it that way.

  • 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-10T03:50:07+00:00Added an answer on June 10, 2026 at 3:50 am

    Try like this:

    ViewBag.ListOfCodes = new SelectList(mymodelinstance.Codes, "Id", "Name");
    ViewBag.Codes = "C";
    

    and in your view:

    <%= Html.DropDownList(
        "Codes", 
        (IEnumerable<SelectListItem>)ViewBag.ListOfCodes, 
        new { style = "max-width: 600px;" }
    ) %>
    

    For this to work you obviously must have an item with Id = “C” inside your collection, like this:

        ViewBag.ListOfCodes = new SelectList(new[]
        {
            new { Id = "A", Name = "Code A" },
            new { Id = "B", Name = "Code B" },
            new { Id = "C", Name = "Code C" },
        }, "Id", "Name");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following (pseudo-) code induces some behaviour which I dont understand. I have 2
I have the following piece of pseudo-C/Java/C# code: int a[]= { 30, 20 };
i have following pseudo code : void siftup(int n) pre condition n>0 && heap(1,n-1)
I have the following problem illustrated by the pseudo code below (might not make
I would like to define the following function in Objective-C. I have provided pseudo-code
I have the following pseudo-SQL schema: table flight id int primary key date timestamp
I have following situation. In a constructor of a pseudo class I attach a
Bit of a strange one this. Please forgive the semi-pseudo code below. I have
I have this code, to get followers and friends(peeps you are following), However I'm
I want your suggestion on the following pseudo-code. Please suggest how could I improve

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.