tags:

views:

63

answers:

1

Hello

I have a view where I'm going to list a checkbox-list (helper), and I'm not sure howto call that, since it always says my "type" is wrong.

I'm trying to call:

public static string CheckBoxList(this HtmlHelper htmlhelper, IEnumerable<string> values, IEnumerable<string> labels, string name)
    {
        return CheckBoxList(htmlhelper, values, labels, name, ((IDictionary<string, object>) null));
    }

And view looks like:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/AdminSite.Master" Inherits="System.Web.Mvc.ViewPage<BookingSystem.MVC.ViewModels.TestViewModel>" %>

Test

<h2>Test</h2>

<table>  
<%= Html.CheckBoxList((IEnumerable<string>)Model.Usergroups, (IEnumerable<string>)Model.Usergroups, "asdf")  %>

<% foreach (var item in Model.Usergroups) { %>
    <tr>
        <td>
            <%= item.UsergroupName %>
        </td>
    </tr>
<% } %>
</table>

<p>
    <%= Html.ActionLink("Create New", "Create") %>
</p>

How can I get this to work, I want my checkbox-list helper to work from different views, so I guess I have to convert the parameters somehow?

/M

+1  A: 

Putting the name of a type in parentheses is called a cast. It doesn't actually "convert" an object into another type. Rather, it switches off type checking at compile time. So if it works at compile time, that proves very little.

In the example code you've posted, there's no good reason to uses casts anywhere. (In particular, you never need to cast null to anything.)

You need to get an IEnumerable<string> from some part of your model? The right way to do that will depend on the structure of your model.

The easiest way to do it in C# 3.0 is to call the Select extension method on an IEnumerable of some type:

var strings = Model.Usergroups.Select(ug => ug.UsergroupName);

Hey presto.

Daniel Earwicker