how to use dropdownlist in mvc

BY LOVE
1-	Create a simple method to get the dropdown value

public List<SelectListItem> GetCategories()
        {
            var ResultCategory = objRKMGEntities.Categories.ToList();
            List<SelectListItem> listCategories = new List<SelectListItem>();
            foreach (var items in ResultCategory)
            {
                listCategories.Add(new SelectListItem() { Text = items.Category_Name, Value = items.Category_Id.ToString() });
            }
            return listCategories;
        }

2-	Call the GetCategories() in action method and store the data in viewbag.

public ActionResult SubCategory()
        {
            ViewBag.Categories = GetCategories();
            return View();
        }

3-	Use the viewbag object in the dropdownlist of the view

@Html.DropDownList("CategoryId", ViewBag.Categories as List<SelectListItem>, "Select Category")

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
mvc dropdownlist in mvc 5 make dropdownlist in asp.net mvc c# list to dropdownlist mvc how to use dropdownlist in asp.net adding a dropdownlist in mvc view create a dropdownlist in C# mvc dropdown asp mvc create a drop down list in mvc website create a dropdownlist in mvc view java script dropdownlist asp.net mvc how to create a drop down list in asp.net mvc 5 asp.net mvc how to create drop down list @html asp.net mvc how to create drop down list html dropdownlist asp.net mvc example mvc @html.dropdownlist @Html.DropDownList( in mvc dropdownlist code in asp.net mvc dropdownlist box asp net mvc html.dropdownlist mvc c# mvc dropdownlist from model MVC net @Html.DropDownList how to create a dropdown list in .net mvc How would you add a dropdownlist to view in MVC? dropdown list asp.net mvc mvc .net dropdown list how to add dropdownlist in asp.net mvc add dropdown list in asp.net mvc dropdownlist asp net mvc cshtml mvc create dropdownlist from database mvc create dropdownlist from guid mvc create dropdownlist from list how to make dropdownlist on asp.net dropdownlist in mvc 5 c# mvc create drop down list how to make drop down list in mvc 5 @html.DropDownList mvc DropDownList asp net MVC dropdownlist in asp mvc c# mvc dropdownlist asp mvc @html.dropdownlist asp mvc create dropdownlist html.dropdownlist in mvc dropdownlist in asp.net mvc mvc dropdown creating dropdownlist in MVC 5 mvc dropdown for asp.net mvc model dropdownlist dropdownlist mvc asp.net c# mvc dropdownlist example how do you code a dropdown list mvc c# mvd dropdown list how to use dropdown list in mvc create dropdown in mvc mvc dropdownlist mvc how do oyu add a dropdown list mvc c# layout vioew simple dropdownlist in mvc how to dropdown in mvc create a dropdown mvc how to add a dropdown list in mvc how to create dropdownlist in mvc dropdown mvc dropdownlist c# mvc mvc dropdownlist using dropdown list in asp.net mvc .net mvc dropdownlist do a drop down asp.net from controller how to set dropdown list value in select html control in mvc using model @html.dropdownlistfor model i cshtml asp.net mvc drop down list dropdown c# mvc dropdownlist in mvc C# dropdownlist in mvc C3 mvc core dropdownlistfor how to create dropdownlist in mvc along with label dropdown asp.net mvc dropdownlist syntax in mvc asp mvc dropdownlist list item dropdown with value in chhtml how to show deropdownlist in mvc aps.ne mvc create dropdown list using html how to create drop down list of values in mvc emplementing the dropdown list in MVC what is the functionality of ddl in asp.net mvc? dropdownlistfor in cshtml mvc how to add drop down in mvc view dropdownlist selected value MVC C# how to make drop down list in mvc how to make drop down listin mvc mvc dropdownlist model list dropdown in controller mvc dropdownlist mvc dropdownlistfor example in mvc bind dropdownlist in mvc mvc c# dropdownlist c# dropdownlist mvc asp.net mvc dropdown mvc 5 dropdownlist selected value mvc drop down list @html.dropdownlist in mvc cshtml dropdownlist c# asp.net create method mvc with dropdownlist from model dropdownlist asp.net mvc dropdownlist mvc example view selected option of dropdownlist mvc MVC Dropdown list asp.net asp net mvc get data on dropdown change drop down list c# asp.net mvc view mvc creating a dropdownlist mvc dropdown list mvc dropdown list for mvc dropdown liast drop down asp.net mvc dropdownlist MVC MS SQL dropdown with model value in mvc how to use dropdown value in model in asp.net mvc drop down view asp.net mvc dropdownfor list code how to enable control with change dropbox in asp.net mvc asp.net mvc dropdownlist how to make a drop down list in mvc dropdownlist asp.net mvc c# html dropdown mvc drop down list mvc how to create drop a drop down list in mvc dropdownlist in mvc dropdown list in mvc dropdown list app in mvc dropdown in mvc how to create a drop down list box in asp mvc dropdown list mvc mvc dropdownlist dropdown list using asp.net mvc html dropdownlist mvc c# asp.net mvc dropdownlist example how to put a dropdown in c# mvc how to create a drop down in mvc dropdown list in html asp.net mvc how to add dropdown in asp.net mvc html dropdown for mvc drop down list change editorfor value in asp.net mvc 5 dropdown list change editorfor value in asp.net mvc 5 dropdown control in asp.net mvc how to use dropdown in mvc how to use dropdownlist in mvc
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source