【问题标题】:MVC SelectList setting width of control in viewMVC SelectList 设置视图中控件的宽度
【发布时间】:2012-10-04 18:41:20
【问题描述】:

感谢帮助我来到这里 - 我的控制器中有以下代码:

 var stands = db.Stands.ToList().Where(s => s.ExhibitorID == null)
              .Select(s => new SelectListItem
              {
                  Value = s.StandID.ToString(),
                  Text = s.Description + "-- £" + s.Rate.ToString()
              });


        ViewBag.StandID = new SelectList(stands, "Value", "Text");

并像这样在我的视图中呈现它:

<div class="editor-field"> 
    @Html.DropDownList("StandID", new {style = "width:150px"})
</div> 

但是在视图中,我收到以下错误:

CS1928:“System.Web.Mvc.HtmlHelper”不包含“DropDownList”的定义和最佳扩展方法重载“System.Web.Mvc.Html.SelectExtensions.DropDownList(System.Web.Mvc.HtmlHelper, string, string)' 有一些无效参数

删除新的 {style=....

如何设置下拉列表的宽度而不出现此错误?

谢谢,

标记

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-mvc-3 razor


    【解决方案1】:

    您需要进行一些更改。首先,改变你的控制器代码:

    ViewBag.StandID = new SelectList(stands, "Value", "Text");
    

    到这里:

    ViewBag.StandList = new SelectList(stands, "Value", "Text");
    

    其次,把你的助手改成这样:

    @Html.DropDownList("StandID", (SelectList)ViewBag.StandList, new {style = "width:150px"})
    

    重要的是要确保html元素名称与item的集合不同,否则会出现各种问题。

    使用强类型视图模型会更好。

    @Html.DropDownListFor(m => m.StandID, Model.StandList, new {style="width:150px;"})
    

    【讨论】:

    • 是否有有效的 DropDownList 接口接受第二个 Int 参数?
    • @BenFinkel - 你正在查看旧版本的帖子,我已经更改了不正确的参数。
    • 不知道我在做什么 - 但关闭,重新加载 - 我不再收到错误 - 非常感谢。
    【解决方案2】:

    您没有将正确的参数传递给@Html.DropDownList。来自此处的 MSDN 文档:http://msdn.microsoft.com/en-us/library/dd470380(v=vs.100).aspx

    您似乎想使用以下重载:

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

    因此,您的第一个参数是正确的名称字符串,但是您需要 SelectList 作为第二个参数,并将 HtmlAttributes 作为第三个参数。试试这样:

    <div class="editor-field">
         @Html.DropDownList("StandID", ViewBag.StandID, new {style = "width:150px"})
    </div> 
    

    更新:

    也不确定您是否将正确的东西传递到您的 ViewBag 中。您将它设置为等于一个新的 SelectList 对象,并且 DropDownList 需要一个 SelectListItems 的集合。

    在你的控制器中试试这个:

    var stands = db.Stands.ToList().Where(s => s.ExhibitorID == null)                  
        .Select(s => new SelectListItem                  
        {                      
            Value = s.StandID.ToString(),                      
            Text = s.Description + "-- £" + s.Rate.ToString()
        });                    
    
    
    ViewBag.StandID = stands;
    

    更新:

    这就是我如何完成同样的事情。我有一个返回 IEnumerable 的静态方法,然后在我的视图中引用该方法。 (对不起VB语法)

    Namespace Extensions
    
    
        Public Module Utilities
    
            Public Function SalutationSelectList(Optional ByVal Salutation As String = "") As IEnumerable(Of SelectListItem)
    
    
                Dim ddl As New List(Of SelectListItem)
    
                ddl.Add(New SelectListItem With {.Text = "", .Value = "", .Selected = If(Salutation = "", True, False)})
                ddl.Add(New SelectListItem With {.Text = "Mr.", .Value = "Mr.", .Selected = If(Salutation = "Mr.", True, False)})
                ddl.Add(New SelectListItem With {.Text = "Ms.", .Value = "Ms.", .Selected = If(Salutation = "Ms.", True, False)})
                ddl.Add(New SelectListItem With {.Text = "Mrs.", .Value = "Mrs.", .Selected = If(Salutation = "Mrs.", True, False)})
                ddl.Add(New SelectListItem With {.Text = "Dr.", .Value = "Dr.", .Selected = If(Salutation = "Dr.", True, False)})
    
                Return ddl
    
            End Function
        End Module
    End Namespace
    
    
    
    @Html.DropDownListFor(Function(org) org.Salutation, SalutationSelectList())
    

    【讨论】:

    • FWIW 我从来没有通过 ViewBag 传递 SelectList,但我认为它没有理由不起作用。
    • 嗨 - 感谢您的尝试 - 我复制/粘贴了它,但得到了同样的错误:CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named ' DropDownList',但似乎具有该名称的扩展方法。扩展方法不能动态调度。考虑强制转换动态参数或在没有扩展方法语法的情况下调用扩展方法。
    猜你喜欢
    • 1970-01-01
    • 2014-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-31
    • 2014-06-18
    相关资源
    最近更新 更多