【问题标题】:Applying Bootstrap styling to MVC dropdownlist将 Bootstrap 样式应用于 MVC 下拉列表
【发布时间】:2015-12-28 20:28:50
【问题描述】:

我正在尝试将样式应用于以下行

@Html.DropDownList("productOptions", "Products")

我改写如下

@Html.DropDownList("productOptions", "Products",  new { @class = "form-control" })

在我的控制器中,我将 productOptions 添加到 ViewData["productOptions"] 并且 Products 是默认文本的可选字符串

但我不断收到以下有关语法的错误消息

Argument 3: cannot convert from 'string' to 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>'  

Cannot resolve method 'Dropdownlist(string, string, { class:string}' candidates are:

非常感谢任何帮助。

【问题讨论】:

标签: asp.net bootstrapping


【解决方案1】:

"Products" 需要是实际对象而不是字符串。

示例用法:

public class ExampleClass //This is the model class
{
    public string SelectedProvider { get; set; }
    public ICollection<System.Web.Mvc.SelectListItem> Providers { get; set; }
}
//The above class you define it this way in your view
@model ExampleClass

//And you use it like this.
@Html.DropDownListFor(model => model.SelectedProvider, Model.Providers, new { @class = "form-control" })

//In your case you have your data in ViewData
@Html.DropDownList("Products", 
new SelectList((IEnumerable) ViewData["productOptions"], "Id", "Name"))

更多信息请看这里:Populating a dropdown from ViewData

【讨论】:

    【解决方案2】:

    试试这个

    List<Product> tempProduct = new List<Product>();
    tempProduct = context.Products.ToList();
    ViewData["tempProduct"] = tempProduct ;
    
    @Html.DropDownList("Select Product",  new SelectList((IEnumerable) ViewData["tempProduct"], "Id", "Name" , new { @class = "form-control" }))
    

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 2017-01-23
      • 2018-07-13
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多