简体   繁体   English

IEnumerable的DropDownList <string> 模型

[英]DropDownList from IEnumerable<string> model

I have a IEnumerable list model containing different city names, I would like to make a DropDownList from this model, I saw an example that work, but for creating links, I think we can do the same thing for a Dropdownlist by looping through the list. 我有一个包含不同城市名称的IEnumerable列表模型,我想从该模型制作一个DropDownList,我看到了一个有效的示例,但是对于创建链接,我认为我们可以通过遍历列表来为Dropdownlist做同样的事情。 How could I do that ? 我该怎么办?

@model IEnumerable<string>





    @foreach (var link in Model)
    {
      // ????
    }

使用已经为您提供的htmlhelper

@Html.DropDownList("cities", new SelectList(Model))

If you don't want to use HtmlHelpers, which, from your question, it sounds like you don't, you can use the following iterative method for an IEnumerable<string> : 如果您不想使用HtmlHelpers,从您的问题来看,这听起来好像您不是,那么可以对IEnumerable<string>使用以下迭代方法:

<select name="myFormInputName">
@foreach (var link in Model)
{
  <option value="@(link)">@(link)</option>
}
</select>

The very simple, and basic form (without moving the SelectList to the controller) is : 非常简单且基本的形式(无需将SelectList移至控制器)是:

@model IEnumerable<string>
@{
var selectList = new SelectList(Model) ;

}

@Html.DropDownList("MyDropDownListName", selectList)

Although I would prefer creating an extension to the HtmlHelper class, or using a "strong named" dropdownlist @Html.DropDownListFor(myvar=>myvar.City, selectList) 尽管我更愿意为HtmlHelper类创建扩展,或使用“强名称”下拉列表@Html.DropDownListFor(myvar=>myvar.City, selectList)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM