mweber
mweber

Reputation: 3

C# ASP.NET MVC 2: HTML.ListBox doesn't work

I want to create a listBox: http://blog.wekeroad.com/blog/aspnet-mvc-preview-using-the-mvc-ui-helpers/

The Html.ListBox doesn't work:

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

I get the following errors:

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>'

Upvotes: 0

Views: 2332

Answers (2)

Jimmy
Jimmy

Reputation: 1

This works:

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

Upvotes: 0

Andrew Bullock
Andrew Bullock

Reputation: 37436

you want:

Update: fixed a bug

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

where "b" is the default selected value

Upvotes: 3

Related Questions