views:

21

answers:

2

If I have a csv of 23,56,78 and I have a whole bunch of checkboxes three of which have these values, is there an easy way to select them?

I suspect the answer is I need to itterate through my list and use a selector but is there another way?

A: 

take a look at http://docs.jquery.com and look up the .find() method. You make jquery return a set of checkboxes that matches an expression (for instance, value = 23, 56 or 78) and then check the boxes..

Yngve B. Nilsen
A: 

I'd probably make an extension method to HtmlHelper that takes a list of all possible values and a list of values that should be selected.

    public static MvcHtmlString CheckBoxList( this HtmlHelper htmlHelper, 
        string baseName, IEnumerable<SelectListItem> allvalues, IEnumerable<string> selectedValues )
    {
        StringBuilder output = new StringBuilder();
        int counter = 0;
        foreach ( var item in allvalues )
        {
            output.AppendLine( htmlHelper.CheckBox( string.Format( "{0}_{1}", baseName, counter++ ), 
                 selectedValues.Contains( item.Value ) ).ToString() );
        }
        return MvcHtmlString.Create( output.ToString() );
    }
dave thieben
sorry, I missed that this was tagged with jQuery. unless you're using AJAX to get the CSV, I'd still try to do it server-side if I could.
dave thieben