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 get domain name information from a Domain using Python
- ModulenotFoundError: no module named 'debug_toolbar' -SOLUTION
- How to create superuser in django project hosted in cPanel without terminal
- CSS & images not loading in django admin | cpanel without terminal
- Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
- How to sell domain name on Godaddy (2023)
- TemplateSyntaxError at / Could not parse the remainder: ' + 1' from 'forloop.counter0 + 1'
- urllib3 v2.0 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'OpenSSL 1.0
Related Article