Check Checkbox based on value in CSV
If I have a csv of 23,56,78 and I have开发者_如何学Go 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?
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..
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() );
}
精彩评论