In this article I am going to explain How to create DropDownList using MVC .net. Here, I am binding the MVC dropdownlist from model object (myDatabaseEntity) using entity framework. You can assign the model object to the viewBag property (Category) and bind on the Razor view using the MVC HTML dropdownlist control.
Create an MVC application in Visual studio.
Create controller:
Right click on the controller folder and click on add and name it as Home.
private myDatabaseEntities db = new myDatabaseEntities();
public ActionResult Index()
{
ViewBag.Category = new SelectList(db.Categories, "CategoryId", "Name");
return View();
}
Create a view Index.cshtml:
Right click on the index view and Create a view. Copy and paste the following code.
Select Department: @Html.DropDownList("Category", "Select Category")
Post your comments / questions
Recent Article
- How to add two numbers in Android Studio? | Source Code
- FindViewByID returns null in android studio -SOLVED
- Saving changes is not permitted in SQL SERVER - [SOLVED]
- Restore of database failed. File cannot be restored over the existing. -[SOLVED]
- One or more projects in the solution were not loaded correctly in Visual Studio 2019 | FIXED
- How to find Laptop's Battery Health?
- SOLVED-Related Field got invalid lookup: icontains error in Django
- How to enable Search Feature in Django admin?
Related Article