MVC dropdown for <>

I just started a project in MVC. I am new to asp.net MVC. I want to add a dropdown as I used to add asp.net.

code flickers in asp.net for dropdownlist window

 <asp:Dropdownlist ID="ddlCity" runat="server"> <asp:ItemList Text="Kolkatta" Value="1"/> <asp:ItemList Text="New Delhi" Value="2"/> <asp:ItemList Text="Mumbai" Value="3"/> <asp:ItemList Text="chennai" Value="4"/> <asp:ItemList Text="Hydrabad" Value="5"/> </asp:Dropdownlist> 

I want a drop down list. So now I need to create a model and controller for this. Suppose I already have a view named "Employee Entry Form" and I need to add one drop-down list only on this page. I do not want to create a model for this drop-down list; I only need this on this page.

+4
source share
4 answers

The values ​​for the drop-down list should be in your existing view model:

 public class WhateverViewModel { // All of your current viewmodel fields here public string SelectedCity { get; set; } public Dictionary<string, string> CityOptions { get; set; } } 

Fill them in your controller with whatever values ​​you want (where SelectedCity is a numeric identifier), and then do the following in your view:

 @Html.DropDownListFor(m => m.SelectedCity, new SelectList(Model.CityOptions, "Key", "Value", Model.SelectedCity)) 

If your values ​​never change, you can make them tough as a static member of your view model, and then do the following:

 @Html.DropDownListFor(m => m.SelectedCity, new SelectList(WhateverViewModel.CityOptions, "Key", "Value", Model.SelectedCity)) 

In any case, this is the data for this view, so it belongs to your model. If you are not using view models, and this view is directly tied to a domain object; you must use them, and now is the right time to start.

+35
source

You can also perform another approach, which should collect all the drop-down lists in 1 auxiliary class and return abstracts, each of which has a static function.

Ex. Helper class

 namespace Asko.Web.Mvc.Infrastructure.Utils { public static class DropdownHelper { public static IEnumerable<SelectListItem> GetAllCategories() { var categories = category.GetAll(); return categories.Select(x => new SelectListItem { Text = x.categoryName, Value = x.categoryId.ToString()})); } } } 

And here you are going to use it on the page:

 <td style="width: 150px;"> <b>Category: </b> <br> @Html.DropDownListFor(m => m.CategoryId, DropdownHelper.GetAllCategories()) </td> 
+7
source

If you want to avoid using the model, you can just use the ViewBag. in your action:

 ViewBag.DirectorId = new SelectList(ListOfItemsToAdd, "dataValueField", "dataTextField", movie.DirectorId); 

and in view:

 <%= Html.DropDownList("VariableNameToPost", ViewBag.DirectorId) //for asp.net view engine %> @Html.DropDownList("VariableNameToPost", ViewBag.DirectorId) // for razor view engine 

and the action that this variable will take will have the following:

 public ActionResult theAction(string VariableNameToPost){...} 

refferences:

+3
source

If you want to create a drop-down shot directly for viewing regardless of the controller and model, you can create it using the following code

 @Html.DropDownList("Departments", new SelectList(new [] { new KeyValuePair<string, int>( "IT", 0 ), new KeyValuePair<string, int>( "HR", 1 )},"Value", "Key")) 
+2
source

Source: https://habr.com/ru/post/1485379/


All Articles