asp.net-mvc 如何在 Razor 语法中创建静态下拉列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30606337/
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-09-07 23:27:34  来源:igfitidea点击:

How to create a static dropdown in Razor syntax?

asp.net-mvchtml.dropdownlistfor

提问by Casey Crookston

After hours of looking on Google and Stack Overflow, I can not find one bloody example of how to build a totally brain dead simple dropdown list that does not come from a database. Honestly, I am having a hard time getting my head around MVC. Can someone please show me how to create this:

在 Google 和 Stack Overflow 上看了几个小时后,我找不到一个关于如何构建一个完全脑死的简单下拉列表的例子,而不是来自数据库。老实说,我很难理解 MVC。有人可以告诉我如何创建这个:

<select name="FooBarDropDown" id="FooBarDropDown">
    <option value="Option1" selected>This is Option 1</option>
    <option value="Option2">This is Option 2</option>
    <option value="Option3">This is Option 3</option>
</select>

Using this:

使用这个:

@Html.DropDownList....

I am looking for an all-in-one-line solution... all in the view. I am having a devil of a time with the syntax.

我正在寻找一种多合一的解决方案……一切尽在眼前。我有一段时间的语法魔鬼。

回答by Brandon O'Dell

I think this is what you are looking for. It would be best though to refactor list construction into view model or in controller.

我想这就是你要找的。最好将列表构造重构到视图模型或控制器中。

@Html.DropDownList("FooBarDropDown", new List<SelectListItem>
{
    new SelectListItem{ Text="Option 1", Value = "1" },
    new SelectListItem{ Text="Option 2", Value = "2" },
    new SelectListItem{ Text="Option 3", Value = "3" },
 }) 

An an example of placing this in the controller might look like this:

将其放置在控制器中的示例可能如下所示:

public ActionResult ExampleView()
{
    var list = new List<SelectListItem>
    {
        new SelectListItem{ Text="Option 1", Value = "1" },
        new SelectListItem{ Text="Option 2", Value = "2" },
        new SelectListItem{ Text="Option 3", Value = "3", Selected = true },
    }; 

    ViewData["foorBarList"] = list;
    return View();
}

And then in your view:

然后在你看来:

@Html.DropDownList("fooBarDropDown", ViewData["list"] as List<SelectListItem>)

If this is truly a static list that you might have to reuse in other views / controllers, then I would consider putting this logic into a static class of sorts. Example:

如果这确实是一个您可能必须在其他视图/控制器中重用的静态列表,那么我会考虑将此逻辑放入静态类中。例子:

public static class DropDownListUtility
{   
    public static IEnumerable<SelectListItem> GetFooBarDropDown(object selectedValue)
    {
        return new List<SelectListItem>
        {
            new SelectListItem{ Text="Option 1", Value = "1", Selected = "1" == selectedValue.ToString()},
            new SelectListItem{ Text="Option 2", Value = "2", Selected = "2" == selectedValue.ToString()},
            new SelectListItem{ Text="Option 3", Value = "3", Selected = "3" == selectedValue.ToString()},
        };             
    }

Which then leaves you a few different ways of accessing the list.

然后,您可以通过几种不同的方式访问列表。

Controller Example:

控制器示例:

public ActionResult ExampleView()
{
    var list = DropDownListUtility.GetFooBarDropDown("2"); //select second option by default;
    ViewData["foorBarList"] = list;
    return View();
}

View Example:

查看示例:

@Html.DropDownList("fooBarDropDown", DropDownListUtility.GetFooBarDropDown("2"))

回答by Jasen

Have a look at the docs for this overload

查看此重载文档

public static MvcHtmlString DropDownList(
  this HtmlHelper htmlHelper,
  string name,
  IEnumerable<SelectListItem> selectList
)

So just add a reference to your List<SelectListItem>()with your options.

因此,只需在List<SelectListItem>()您的选项中添加对您的引用即可。

List<SelectListItem> items = new List<SelectListItem>();
 items.Add(new SelectListItem { Text = "Option1", Value = "Option1"});
 items.Add(new SelectListItem { Text = "Option2", Value = "Option2" });
 items.Add(new SelectListItem { Text = "Option3", Value = "Option3", Selected = true });

You can even embed that in your view if you don't want to pass it from your controller.

如果您不想从控制器传递它,您甚至可以将它嵌入到您的视图中。

@{
    List<SelectListItem> items = ...
}

Then use it

然后使用它

@Html.DropDownList("FooBarDropDown", items)