C#ASP.NET MVC 2:HTML.ListBox不起作用

时间:2010-08-26 09:16:37

标签: c# asp.net asp.net-mvc listbox

我想创建一个listBox: http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/

Html.ListBox不起作用:

<div class="editor-field">
    <%
        string[] movies = new string [] { "a", "b", "c" };
    %>
    <%: Html.ListBox("lala", movies, new string[] { "b" })%>
</div>

我收到以下错误:

Error 1 'System.Web.Mvc.HtmlHelper<Transponder.Models.EditUserModel>' does not contain a definition for 'ListBox' and the best extension method overload 'System.Web.Mvc.Html.SelectExtensions.ListBox(System.Web.Mvc.HtmlHelper, string, System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>, System.Collections.Generic.IDictionary<string,object>)' has some invalid arguments 
Error 2 Argument 3: cannot convert from 'string[]' to 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>'
Error 3 Argument 4: cannot convert from 'string[]' to 'System.Collections.Generic.IDictionary<string,object>'

2 个答案:

答案 0 :(得分:3)

你想要:

更新:修复了错误

<%: Html.ListBox("lala", new SelectList(movies, "b"))%>

其中“b”是默认选定值

答案 1 :(得分:0)

这有效:

<%: Html.ListBox("lala", new SelectList(new string[] { "a", "b", "c" }), new SelectList(new string[] { "b" }))%>