Hi nauna,
Using the article i have created the example.
Check this example. Now please take its reference and correct your code.
Model
public class HobbyModel
{
public bool IsSelected { get; set; }
public int Id { get; set; }
public string Name { get; set; }
}
Namespaces
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
List<HobbyModel> hobbies = GetHobbies();
return View(hobbies);
}
private static List<HobbyModel> GetHobbies()
{
List<HobbyModel> hobbies = new List<HobbyModel>();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string query = "SELECT HobbyId,Hobby,IsSelected FROM Hobbies";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
hobbies.Add(new HobbyModel
{
Id = Convert.ToInt32(sdr["HobbyId"]),
Name = sdr["Hobby"].ToString(),
IsSelected = Convert.ToBoolean(sdr["IsSelected"])
});
}
}
con.Close();
}
}
return hobbies;
}
[HttpPost]
public ActionResult Save(List<HobbyModel> selected)
{
string _searches = "";
foreach (HobbyModel hobby in selected)
{
if (hobby.IsSelected)
{
_searches += hobby.Id.ToString() + ",";
}
}
string url = string.Format("/Home/Search?search={0}", _searches);
return Redirect(url);
}
public ActionResult Search()
{
List<HobbyModel> hobbies = GetHobbies();
string[] checkedHobbyId = Request.QueryString["search"].Split(',');
for (int i = 0; i < hobbies.Count; i++)
{
HobbyModel hobby = hobbies[i];
for (int j = 0; j < checkedHobbyId.Length - 1; j++)
{
if (hobby.Id == Convert.ToInt32(checkedHobbyId[j]))
{
hobby.IsSelected = true;
break;
}
}
}
// Returning updated model.
// You can return PartialView with the Updated model.
return View(hobbies);
}
}
View
Index
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<HobbyModel>>" %>
<%@ Import Namespace="_Comma_Seperated_Value_Checked.Models" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<%using (Html.BeginForm("Save", "Home", FormMethod.Post))
{%>
<table>
<%for (int i = 0; i < Model.Count(); i++)
{%>
<tr>
<td>
<%:Html.HiddenFor(m => ((List<HobbyModel>)Model)[i].Id)%>
<%:Html.CheckBoxFor(m => ((List<HobbyModel>)Model)[i].IsSelected, new { onclick = "this.form.submit()" })%>
<%:Html.DisplayFor(m => ((List<HobbyModel>)Model)[i].Name)%>
</td>
</tr>
<%}%>
</table>
<%} %>
</body>
</html>
Search
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<HobbyModel>>" %>
<%@ Import Namespace="_Comma_Seperated_Value_Checked.Models" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Search</title>
</head>
<body>
<table>
<%for (int i = 0; i < Model.Count(); i++)
{%>
<tr>
<td>
<%:Html.HiddenFor(m => ((List<HobbyModel>)Model)[i].Id)%>
<%:Html.CheckBoxFor(m => ((List<HobbyModel>)Model)[i].IsSelected)%>
<%:Html.DisplayFor(m => ((List<HobbyModel>)Model)[i].Name)%>
</td>
</tr>
<%}%>
</table>
</body>
</html>
Screenshot