Skip to content Skip to sidebar Skip to footer

Get Comma-separated String From CheckboxList HTML Helper

I got the following code from the internet for CheckboxListFor html helper extension. At the moment, in the SelectedValues it is returning a List of selected values f

Solution 1:

I prefer changing the code of your view model:

public class MyViewModel
{
    public SelectList MySelectList{ get; set; }

    public List<string> SelectedValues { get; set; }

    public string SelectedString
    {
        get
        {
            if (SelectedValues == null) return "";
            return string.Join(",", SelectedValues);
        }
        set
        {
            if (!string.IsNullOrEmpty(value))
            {
                SelectedValues = value.Split(',').ToList();
            }
        }
    }
}

You can get the comma seperated string :

//In controller
var selected = model.SelectedString; 

//In view
searchCriteria["model.SelectedValues"] = Model.SelectedString;

Post a Comment for "Get Comma-separated String From CheckboxList HTML Helper"