MVC Dropdownlistfor<>

后端 未结 4 664
轮回少年
轮回少年 2021-02-10 14:41

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

code glimpses in asp.net for Dropdownlist box

相关标签:
4条回答
  • 2021-02-10 15:06

    The values for the dropdownlist 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; }
    }
    

    Populate these in your controller with whatever values you want (where SelectedCity is your numerical ID), 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 could hardcode them as a static member of your view model and then do:

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

    Either way, this is data for this view, so it belongs in your view model. If you're not using view models and this view is directly tied to a domain entity; you should be using them and now is as good a time as any to start.

    0 讨论(0)
  • 2021-02-10 15:12

    If you want to create a drop down directly on view regardless of controller and model then you can create it using following code

    @Html.DropDownList("Departments", 
      new SelectList(new [] { new KeyValuePair<string, int>( "IT", 0 ), new KeyValuePair<string, int>( "HR", 1 )},"Value", "Key")) 
    
    0 讨论(0)
  • 2021-02-10 15:14

    You can also follow another approach which is to collect all dropdown lists in 1 helper class and return theses lists each with a static function.

    Ex. Dropdown 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 in the page:

    <td style="width: 150px;">
          <b>Category: </b>
          <br>
     @Html.DropDownListFor(m => m.CategoryId, DropdownHelper.GetAllCategories())
    </td>
    
    0 讨论(0)
  • 2021-02-10 15:20

    If you want to avoid the usage of model, you may simply use a ViewBag. in your action:

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

    and in the view:

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

    and the action that would accept the variable would then have something like this:

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

    refferences:

    • Select List
    • Drop Down List
    0 讨论(0)
提交回复
热议问题