How to declare a drop-down list in asp.net MVC?
Loading
How to declare a drop-down list in asp.net MVC?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 21, 2024, 9:15 AM EST
Hi Samia,
// Model public class MyViewModel { public int SelectedDirection { get; set; } public IEnumerable Directions { get; set; } } // Controller public ActionResult MyAction() { var model = new MyViewModel { Directions = new List { new SelectListItem { Value = "1", Text = "Direction 1" }, new SelectListItem { Value = "2", Text = "Direction 2" }, new SelectListItem { Value = "3", Text = "Direction 3" } } }; return View(model); } // View (Razor) @model MyViewModel Direction de willaya @Html.DropDownListFor(m => m.SelectedDirection, Model.Directions, "Select a Direction", new { @id = "DllDIW", @class = "form-control" })Ramesh PalanivelRank #372 · 6.8K reputationPosted Nov 21, 2024, 8:14 AM EST
Please refer this link
https://www.c-sharpcorner.com/UploadFile/3d39b4/dropdownlist-in-Asp-Net-mvc/
https://www.pluralsight.com/resources/blog/guides/asp-net-mvc-populating-dropdown-lists-in-razor-views-using-the-mvvm-design-pattern-entity-framework-and-ajax
https://www.c-sharpcorner.com/UploadFile/sourabh_mishra1/cascading-dropdownlist-in-Asp-Net-mvc/
Jayraj ChhayaRank #304 · 8.2K reputationPosted Nov 21, 2024, 8:15 AM EST
Hi samia,
In ASP.NET MVC, declaring a drop-down list can be accomplished using the
Html.DropDownListForhelper method. This method binds a model property to a drop-down list, allowing for easy data binding and retrieval. Below is a simple example demonstrating how to create a drop-down list.First, ensure you have a model that contains the property for the selected value and a list of items for the drop-down:
public class MyViewModel { public int SelectedItemId { get; set; } public IEnumerable Items { get; set; } }In your controller, populate the model and pass it to the view:
public ActionResult Create() { var model = new MyViewModel { Items = new List { new SelectListItem { Value = "1", Text = "Item 1" }, new SelectListItem { Value = "2", Text = "Item 2" }, new SelectListItem { Value = "3", Text = "Item 3" } } }; return View(model); }Finally, in your view, use the
Html.DropDownListFormethod to render the drop-down list:This code will create a drop-down list that allows users to select an item, with the selected value bound to the
SelectedItemIdproperty of your model.samia34 reputationPosted Nov 21, 2024, 8:25 AM EST
you can help me to correct this code
Vishal YelveRank #178 · 13.6K reputationPosted Nov 21, 2024, 10:48 AM EST
Hey Samia,
do refer below article
https://www.c-sharpcorner.com/article/dropdownlist-in-asp-net-mvc/
Jignesh KumarRank #91 · 22.9K reputationPosted Nov 21, 2024, 10:45 PM EST
Hello samia,
Here is the example to create Country Dropdown list in asp .net MVC,
1. Create Country Model
public class CountryViewModel { public string SelectedCountry { get; set; } // selected country public IEnumerable Countries { get; set; } // To hold the list of countries }2. Create a Controller with method,
public class HomeController : Controller { public ActionResult Index() { var model = new CountryViewModel { Countries = new List { new SelectListItem { Text = "United States", Value = "US" }, new SelectListItem { Text = "Canada", Value = "CA" }, new SelectListItem { Text = "United Kingdom", Value = "UK" }, new SelectListItem { Text = "India", Value = "IN" }, new SelectListItem { Text = "Australia", Value = "AU" } } }; return View(model); } [HttpPost] public ActionResult Index(CountryViewModel model) { // Fetch the selected country var selectedCountry = model.SelectedCountry; // Display selected country message in Viewbag ViewBag.Message = $"You selected: {selectedCountry}"; // Repopulate the Countries list to redisplay the dropdown model.Countries = new List { new SelectListItem { Text = "United States", Value = "US" }, new SelectListItem { Text = "Canada", Value = "CA" }, new SelectListItem { Text = "United Kingdom", Value = "UK" }, new SelectListItem { Text = "India", Value = "IN" }, new SelectListItem { Text = "Australia", Value = "AU" } }; return View(model); } }3. Create a view to display country dropdown in your web,
@model CountryViewModel @using (Html.BeginForm()) { Select Country: @Html.DropDownListFor( model => model.SelectedCountry, Model.Countries, "Please Select a Country", new { @class = "form-control" } ) Submit @if (ViewBag.Message != null) { @ViewBag.Message } }