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.Submit Form on CheckBox Click using jQuery 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 title="Save (Insert) multiple CheckBoxes (CheckBoxList) values to database in ASP.Net MVC'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><strong><span style="text-decoration: underline;">Model</span></strong></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;">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; items = new List&lt;HobbyModel&gt;(); string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { string query = &#34;SELECT HobbyId,Hobby,IsSelected FROM Hobbies&#34;; using (SqlCommand cmd = new SqlCommand(query)) { cmd.Connection = con; con.Open(); using (SqlDataReader sdr = cmd.ExecuteReader()) { while (sdr.Read()) { items.Add(new HobbyModel { Id = Convert.ToInt32(sdr[&#34;HobbyId&#34;]), Name = sdr[&#34;Hobby&#34;].ToString(), IsSelected = Convert.ToBoolean(sdr[&#34;IsSelected&#34;]) }); } } con.Close(); } } return items; } [HttpPost] public ActionResult Index(string checkedHobbies) { string[] checkedCheckBoxes = checkedHobbies.Split(&#39;,&#39;); List&lt;HobbyModel&gt; hobbies = GetHobbies(); for (int i = 0; i &lt; hobbies.Count; i++) { HobbyModel hobby = hobbies[i]; for (int j = 0; j &lt; checkedCheckBoxes.Length - 1; j++) { if (hobby.Id == Convert.ToInt32(checkedCheckBoxes[j])) { hobby.IsSelected = true; break; } } } return View(hobbies); } }</pre> <p><strong><span style="text-decoration: underline;">View</span></strong></p> <pre class="brush: html">&lt;%@ Page Language=&#34;C#&#34; Inherits=&#34;System.Web.Mvc.ViewPage&lt;IEnumerable&lt;_Submit_Form_CheckBox_Checked_MVC.Models.HobbyModel&gt;&gt;&#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;script type=&#34;text/javascript&#34; src=&#34;https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js&#34;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34;&gt; $(function () { $(&#34;input[type=checkBox]&#34;).on(&#39;click&#39;, function () { var hobbies = &#34;&#34;; $(&#34;input[type=checkbox]:checked&#34;).each(function (index, item) { hobbies += $(item).closest(&#39;tr&#39;).find(&#39;#item_Id&#39;).val() + &#34;,&#34;; }); $(&#39;#checkedCheckBoxes&#39;).val(hobbies); this.form.submit(); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;%using (Html.BeginForm(&#34;Index&#34;, &#34;Home&#34;, FormMethod.Post)) {%&gt; &lt;input type=&#34;hidden&#34; id=&#34;checkedCheckBoxes&#34; name=&#34;checkedHobbies&#34; /&gt; &lt;table&gt; &lt;%foreach (var item in Model) {%&gt; &lt;tr&gt; &lt;td&gt; &lt;%:Html.CheckBoxFor(m =&gt; item.IsSelected)%&gt; &lt;%:Html.DisplayFor(m =&gt; item.Name)%&gt; &lt;%:Html.HiddenFor(m =&gt; item.Id)%&gt; &lt;/td&gt; &lt;/tr&gt; &lt;%} %&gt; &lt;/table&gt; &lt;%} %&gt; &lt;/body&gt; &lt;/html&gt; </pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/191218/Submit-Form-on-CheckBox-Click-using-jQuery-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/191218/Submit-Form-on-CheckBox-Click-using-jQuery-in-ASPNet-MVC/Wed, 15 May 2019 07:42:47 GMT