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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:33:52+00:00 2026-06-09T08:33:52+00:00

I need to know how I could create a drop down list to represent

  • 0

I need to know how I could create a drop down list to represent all the categories in my “Categories” table.

I have already extracted the names and the values of each category I need, using this LINQ query :

var dbcontext = new LNQ2SQLDataContext();
        var Q = from P in dbcontext.Categories
                where P.SUB_CAT == null
                select P;

I can pass this “Q” to my view like this :
In Controller :

return View(Q);

And in the View :

@model IEnumerable<MyAppName.Models.Category>

But I have no idea how to use @html.DropDownListFor() to make a darn good drop down list out of the model. 😐

PLUS:

I could make a SelectList from the query “Q” like this :

var category_list = new SelectList(Q, "CAT_ID", "CAT_Name");

BUT I don’t know how to create a drop down list (without using ViewBag to pass the category_list to the view) from a simple SelectList, either 😐

I searched through as many blogs and websites as I could. But they didn’t have the solution for my problem. I only got more and more confused!

So can anybody help please ? :/

  • 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-09T08:33:55+00:00Added an answer on June 9, 2026 at 8:33 am

    To use DropDownListFor you’ll either have to have a model that has a SelectList or data to make a selectlist out of and a property to store the selected value of the dropdown OR use the ViewBag to pass the category_list. So you can go with…

    Public Class MyViewModel
    {
        Public Integer SelectedCategory { get; set; }
        Public SelectList Categories { get; set; }
    }
    
    Public Class ItemsController : Controller
    {
        Public ActionResult Index()
        {
            var dbcontext = new LNQ2SQLDataContext();
            var Q = from P in dbcontext.Categories
                    where P.SUB_CAT == null
                    select P;
            var vm = new MyViewModel();
            vm.Categories = new SelectList(Q, "CategoryID", "Name");
            return View(vm);
        }
    
        [HttpPost()]
        Public ActionResult Index(MyViewModel vm)
        {
            var theSelectedCategory = vm.SelectedCategory;
        }
    }
    

    The view would be…

    @model MyViewModel
    @Html.DropDownListFor(model => model.SelectedCategory, Model.Categories, "Select Category")
    

    Note: I don’t typically code in C# so I can’t guarantee the syntax is exactly right.

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

Sidebar

Related Questions

I am implementing a simple drop-down using hiccup: ;DATASET/CREATE (defn get-cols-nms [table] This function
Need to know this so that i could send DTMF and that is going
I need to know how to change background of expanded list item when it
I couldn't find a straight answer to my question and need to know it
Need to know which event triggered after text selection done in Apple iPad. It
I need to know how to exclude files/folders from php generated zip. Here is
I need to know on a continuous basis when my UIScrollView is scrolling or
I need to know a couple of things, concerning IRC servers that I couldnt
I need to know how many instances of my process are running on a
I need to know which iOS device is currently running app (saying more exactly

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.