C# <>的MVC下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17019272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:21:44  来源:igfitidea点击:

MVC Dropdownlistfor<>

c#asp.netasp.net-mvcasp.net-mvc-2

提问by Shahnawaz

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.

我刚刚在 MVC 中开始了一个项目。我是 MVC asp.net 的新手。我想添加一个下拉列表框,就像我以前在asp.net中添加的那样。

code glimpses in asp.net for Dropdownlist box

下拉列表框在asp.net中的代码一瞥

<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 dropdown list box in that manner.... meaning, now I need to create a model and controller for that. Suppose I have already have a view named "Employee Entry Form" and I need to add one dropdown list only on that page. I do not want to create a model for this dropdown list; I only need it on this page.

我想要一个这样的下拉列表框......意思是,现在我需要为此创建一个模型和控制器。假设我已经有一个名为“Employee Entry Form”的视图,我只需要在该页面上添加一个下拉列表。我不想为此下拉列表创建模型;我只在这个页面上需要它。

回答by Igarioshka

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

如果你想避免使用模型,你可以简单地使用一个 ViewBag。在你的行动中:

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:

参考资料:

回答by Ant P

The values for the dropdownlist should be in your existingview 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:

在您的控制器中填充您想要的任何值(其中 SelectedCity 是您的数字 ID),然后在您的视图中执行以下操作:

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

If your values never change, you could hardcode them as a staticmember of your view model and then do:

如果您的值永远不会改变,您可以将它们硬编码为static您的视图模型的成员,然后执行以下操作:

@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.

无论哪种方式,这是此视图的数据,因此它属于您的视图模型。如果您不使用视图模型并且此视图直接绑定到域实体;你应该使用它们,现在是开始的好时机。

回答by Shaikh Farooque

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")) 

回答by Bishoy Hanna

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.

您还可以遵循另一种方法,即收集 1 个帮助程序类中的所有下拉列表,并使用静态函数返回每个列表。

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>