Get selected (checked) CheckBox values as Comma Separated String in Controller in ASP.Net MVC

Last Reply one month ago By nauna

Posted one month ago

hello,

i have this checkbox list bind with list working fine

on button submit even i want to get all the selected checkboxlist value as comma seperated in my string on my controller 

@model IEnumerable<MVCApp.Models.FirstCategories>
@using (@Html.BeginForm("CheckboxListTopcategoryMenu_post", "Application", FormMethod.Get))
{  
foreach (var item in Model)
{
    @Html.CheckBoxFor(m => item.isSelected)
    @Html.DisplayFor(m => item.Title)
    @Html.HiddenFor(m => item.id)
}
<input id="Button2" type="submit" class="btn red" value="Search" />
}
        public ActionResult CheckboxListTopcategoryMenu_post(List<FirstCategories> categoryselected)
        {
            string _searches = "";
            foreach (FirstCategories cat in categoryselected)
            {
                _searches = cat.isSelected.ToString();
            }
            return View();
        }

but it gives error pls advice

Posted one month ago

i have found the solution

controller

        public ActionResult CheckboxListTopcategoryMenu_post(List<FirstCategories> categoryselected)
        {
            string _searches = "";
            foreach (FirstCategories cat in categoryselected)
            {
                _searches += cat.id.ToString() + ",";
            }
            string url = string.Format("/ads/search?search={0}", _searches);
            return Redirect(url);
        }

 

@model List<MVCApp.Models.FirstCategories>
@using (@Html.BeginForm("CheckboxListTopcategoryMenu_post", "Application", FormMethod.Get))
{
    for (int i = 0; i < Model.Count(); i++)
    {
        @Html.HiddenFor(m => m[i].id)
        @Html.HiddenFor(m => m[i].Title)
        @Html.CheckBoxFor(m => m[i].isSelected)
        @Html.DisplayFor(m => m[i].Title)
    }    
    <input id="Button2" type="submit" class="btn red" value="Search" />
}