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.Check Uncheck CheckBox on Button Click with Button Text in ASP.Net MVC<p>Hi&nbsp;chetan,</p> <p>Check this example. Now please take its reference and correct your code.</p> <p><strong><span style="text-decoration: underline;">Database</span></strong></p> <pre class="brush: sql">CREATE TABLE EmailSubscription(Id INT IDENTITY PRIMARY KEY,isemailon_off INT) INSERT INTO EmailSubscription VALUES(0) </pre> <p><strong><span style="text-decoration: underline;">Model</span></strong></p> <pre class="brush: csharp">public class CheckUnCheckModel { public string ButtonText { get; set; } public bool CheckUnCheck { 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() { CheckUnCheckModel model = BindCheckBoxButton(); return View(model); } private static CheckUnCheckModel BindCheckBoxButton() { TestEntities entities = new TestEntities(); EmailSubscription email = entities.EmailSubscriptions.FirstOrDefault(); CheckUnCheckModel model = new CheckUnCheckModel(); model.ButtonText = Convert.ToBoolean(email.isemailon_off) ? &#34;Turn Email Off&#34; : &#34;Turn Email On&#34;; model.CheckUnCheck = Convert.ToBoolean(email.isemailon_off); return model; } [HttpPost] public ActionResult Index(string submit) { TestEntities entities = new TestEntities(); EmailSubscription email = entities.EmailSubscriptions.FirstOrDefault(); email.isemailon_off = submit.ToLower() == &#34;turn email on&#34; ? 1 : 0; entities.SaveChanges(); CheckUnCheckModel model = BindCheckBoxButton(); return View(model); } }</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;CheckUnCheckModel&gt;&#34; %&gt; &lt;%@ Import Namespace=&#34;_834192_Check_UnCheck_CheckBox.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;Index&#34;, &#34;Home&#34;, FormMethod.Post)) { %&gt; &lt;input type=&#34;submit&#34; value=&#39;&lt;%= Model.ButtonText%&gt;&#39; name=&#34;submit&#34; /&gt; &lt;input name=&#34;CheckUnCheck&#34; &lt;%=(Convert.ToBoolean(Model.CheckUnCheck) ? &#34;checked=&#39;checked&#39;&#34; : string.Empty)%&gt; type=&#34;checkbox&#34; /&gt; &lt;%} %&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/b4vZKEP.gif" alt="" width="249" height="101" /></span></strong></p>https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/Thu, 04 Jul 2019 05:37:18 GMTCheck Uncheck CheckBox on Button Click with Button Text in ASP.Net MVC<p>Hi Sir,i used the Following Approach: .cshtml</p> <pre class="brush: js">&lt;div class=&#34;col-md-8 form-group&#34; style=&#34;text-align:right;&#34;&gt; &lt;input type=&#34;button&#34; value=&#34;Turn Email ON&#34; class=&#34;btn btn-success&#34; id=&#34;btnturnemailonoff&#34; /&gt; &lt;/div&gt; &lt;div class=&#34;col-md-2 form-group&#34;&gt; &lt;input type=&#34;checkbox&#34; id=&#34;Chkemailonoff&#34; /&gt; &lt;/div&gt; &lt;script type=&#34;javascript&#34;&gt; $(document).ready(function () { $(&#39;#btnturnemailonoff&#39;).on(&#39;click&#39;, function () { var checked = !$(this).data(&#39;checked&#39;); $(&#34;#Chkemailonoff&#34;).prop(&#39;checked&#39;, checked); $(this).val(checked ? &#39;Turn Email Off&#39; : &#39;Turn Email on&#39;) $(this).data(&#39;checked&#39;, checked); debugger; var url = &#39;@Url.Action(&#34;CheckEmail&#34;, &#34;BillingDetails&#34;)&#39;; $.ajax({ url: url, type: &#34;POST&#34;, data: { checkemails: checked }, dataType: &#34;json&#34;, // traditional: true, success: function () { alert(&#34;ajax request to server succeed&#34;); } }); }); }); &lt;/script&gt;</pre> <p>Got Struck here: Controller&nbsp;</p> <pre class="brush: csharp">[HttpPost] public ActionResult CheckEmail(string checkemails) { //how to use SP return new JsonResult { }; }</pre> <p>&nbsp;</p>https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/Thu, 04 Jul 2019 06:17:51 GMTCheck Uncheck CheckBox on Button Click with Button Text in ASP.Net MVC<p>Refer below articles.</p> <h1 class="header"><a title="Entity Framework: Call Stored Procedure with Parameters using C# and VB.Net'a" href="https://www.aspsnippets.com/Articles/Entity-Framework-Call-Stored-Procedure-with-Parameters-using-C-and-VBNet.aspx">Entity Framework: Call Stored Procedure with Parameters using C# and VB.Net</a></h1> <h1 class="header"><a title="Select Insert Edit Update and Delete (CRUD) using Single Stored Procedure in ASP.Net MVC'a" href="https://www.aspsnippets.com/Articles/Select-Insert-Edit-Update-and-Delete-CRUD-using-Single-Stored-Procedure-in-ASPNet-MVC.aspx">Select Insert Edit Update and Delete (CRUD) using Single Stored Procedure in ASP.Net MVC</a></h1>https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/Thu, 04 Jul 2019 23:42:02 GMTCheck Uncheck CheckBox on Button Click with Button Text in ASP.Net MVC<p>Finally worked..:)</p> <pre class="brush: csharp"> &lt;div class=&#34;col-md-8 form-group&#34; style=&#34;text-align:right;&#34;&gt; &lt;input type=&#34;button&#34; value=&#34;Turn Email ON&#34; class=&#34;btn btn-success&#34; id=&#34;btnturnemailonoff&#34; /&gt; &lt;/div&gt; &lt;div class=&#34;col-md-2 form-group&#34;&gt; &lt;input type=&#34;checkbox&#34; id=&#34;Chkemailonoff&#34; /&gt; &lt;/div&gt; &lt;script type=&#34;javascript&#34;&gt; $(document).ready(function () { $(&#39;#btnturnemailonoff&#39;).on(&#39;click&#39;, function () { var checked = !$(this).data(&#39;checked&#39;); $(&#34;#Chkemailonoff&#34;).prop(&#39;checked&#39;, checked); $(this).val(checked ? &#39;Turn Email Off&#39; : &#39;Turn Email on&#39;) $(this).data(&#39;checked&#39;, checked); debugger; var url = &#39;@Url.Action(&#34;CheckEmail&#34;, &#34;BillingDetails&#34;)&#39;; $.ajax({ url: url, type: &#34;POST&#34;, data: { checkemails: checked }, dataType: &#34;json&#34;, // traditional: true, success: function () { alert(&#34;ajax request to server succeed&#34;); } }); }); }); &lt;/script&gt;</pre> <p>Update model from the database...&nbsp;&nbsp;</p> <p>Controller code:</p> <pre class="brush: csharp"> [HttpPost] public ActionResult CheckEmail(string checkemails) { //calling Stored Procedure if (!string.IsNullOrWhiteSpace(checkemails)) { var checkemails1 = new SqlParameter(&#34;checkemails&#34;, checkemails); db.Database.ExecuteSqlCommand(&#34;EXEC Sp_Email_on_off @checkemails&#34;, checkemails1); } return new JsonResult { }; }</pre> <p>Thank u so much Sir for the Help...highly appreciated...:)</p>https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/834192/Check-Uncheck-CheckBox-on-Button-Click-with-Button-Text-in-ASPNet-MVC/Fri, 05 Jul 2019 04:06:16 GMT