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:

publicclassMyViewModel
{
    public SelectList MySelectList{ get; set; }

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

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

You can get the comma seperated string :

//In controllervar selected = model.SelectedString; 

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

Post a Comment for "Get Comma-separated String From Checkboxlist Html Helper"