ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Retain CheckBoxList selection on Form Submit in ASP.Net MVC<p>Hi&nbsp;<a class="username" rel="nauna">nauna,</a></p> <p>Using the article i have created the example.</p> <h1 class="header"><a href="https://www.aspsnippets.com/Articles/Save-Insert-multiple-CheckBoxes-CheckBoxList-values-to-database-in-ASPNet-MVC.aspx">Save (Insert) multiple CheckBoxes (CheckBoxList) values to database in ASP.Net MVC</a></h1> <p>Check this example. Now please take its reference and correct your code.</p> <p><span style="text-decoration: underline;"><strong>Model</strong></span></p> <pre class="brush: csharp">public class HobbyModel { public bool IsSelected { get; set; } public int Id { get; set; } public string Name { get; set; } }</pre> <p><strong><span style="text-decoration: underline;">Namespaces</span></strong></p> <pre class="brush: csharp">using System.Collections.Generic; using System.Configuration; using System.Data.SqlClient;</pre> <p><strong><span style="text-decoration: underline;">Controller</span></strong></p> <pre class="brush: csharp">public class HomeController : Controller { // GET: /Home/ public ActionResult Index() { List&lt;HobbyModel&gt; hobbies = GetHobbies(); return View(hobbies); } private static List&lt;HobbyModel&gt; GetHobbies() { List&lt;HobbyModel&gt; hobbies = new List&lt;HobbyModel&gt;(); 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&lt;HobbyModel&gt; 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&lt;HobbyModel&gt; hobbies = GetHobbies(); string[] checkedHobbyId = Request.QueryString["search"].Split(','); for (int i = 0; i &lt; hobbies.Count; i++) { HobbyModel hobby = hobbies[i]; for (int j = 0; j &lt; 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); } }</pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <p><strong>Index</strong></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; Inherits=&#34;System.Web.Mvc.ViewPage&lt;IEnumerable&lt;HobbyModel&gt;&gt;&#34; %&gt; &lt;%@ Import Namespace=&#34;_Comma_Seperated_Value_Checked.Models&#34; %&gt; &lt;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;%using (Html.BeginForm(&#34;Save&#34;, &#34;Home&#34;, FormMethod.Post)) {%&gt; &lt;table&gt; &lt;%for (int i = 0; i &lt; Model.Count(); i++) {%&gt; &lt;tr&gt; &lt;td&gt; &lt;%:Html.HiddenFor(m =&gt; ((List&lt;HobbyModel&gt;)Model)[i].Id)%&gt; &lt;%:Html.CheckBoxFor(m =&gt; ((List&lt;HobbyModel&gt;)Model)[i].IsSelected, new { onclick = &#34;this.form.submit()&#34; })%&gt; &lt;%:Html.DisplayFor(m =&gt; ((List&lt;HobbyModel&gt;)Model)[i].Name)%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;%}%&gt; &lt;/table&gt; &lt;%} %&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><strong>Search</strong></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; Inherits=&#34;System.Web.Mvc.ViewPage&lt;IEnumerable&lt;HobbyModel&gt;&gt;&#34; %&gt; &lt;%@ Import Namespace=&#34;_Comma_Seperated_Value_Checked.Models&#34; %&gt; &lt;!DOCTYPE html PUBLIC &#34;-//W3C//DTD XHTML 1.0 Transitional//EN&#34; &#34;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&#34;&gt; &lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;Search&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;table&gt; &lt;%for (int i = 0; i &lt; Model.Count(); i++) {%&gt; &lt;tr&gt; &lt;td&gt; &lt;%:Html.HiddenFor(m =&gt; ((List&lt;HobbyModel&gt;)Model)[i].Id)%&gt; &lt;%:Html.CheckBoxFor(m =&gt; ((List&lt;HobbyModel&gt;)Model)[i].IsSelected)%&gt; &lt;%:Html.DisplayFor(m =&gt; ((List&lt;HobbyModel&gt;)Model)[i].Name)%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;%}%&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p><strong><span style="text-decoration: underline;">Screenshot</span></strong></p> <p><strong><span style="text-decoration: underline;"><img src="https://i.imgur.com/Jop69Gz.gif" alt="" width="472" height="233" /></span></strong></p>https://www.aspforums.net:443/Threads/123491/Retain-CheckBoxList-selection-on-Form-Submit-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/123491/Retain-CheckBoxList-selection-on-Form-Submit-in-ASPNet-MVC/Thu, 16 May 2019 05:15:37 GMT