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.Change Button Text and CheckBox checked unchecked based on Database value in ASP.Net MVC<p>Hi&nbsp;chetan,</p> <blockquote><cite>chetan says:</cite> <pre><code class="js keyword">var</code> <code class="js plain">checked = !$(</code><code class="js keyword">this</code><code class="js plain">).data(</code><code class="js string">'checked'</code><code class="js plain">);</code></pre> </blockquote> <p>Your problem is with the above line of code.</p> <p>When page refreshed you will always get true for data attribute.</p> <p>So instead of using data attribute use the checkbox checked property to check the value and based on that update the code.</p> <p>Check this example. Now please take its reference and correct your code.</p> <p>Here i have used entity framework to update the record in database. You need to change with your stored procedure code.</p> <p><span style="text-decoration: underline;"><strong>Database</strong></span></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;">Controller</span></strong>&nbsp;</p> <pre class="brush: csharp">public class HomeController : Controller { // GET: /Home/ public ActionResult Index() { TestEntities db = new TestEntities(); return View(db.EmailSubscriptions.FirstOrDefault()); } [HttpPost] public ActionResult CheckEmail(string checkemails) { if (!string.IsNullOrWhiteSpace(checkemails) || checkemails == &#34;true&#34; || checkemails == &#34;false&#34;) { TestEntities db = new TestEntities(); int status = 0; if (checkemails.ToLower() == &#34;true&#34;) { status = 1; } EmailSubscription email = db.EmailSubscriptions.FirstOrDefault(); email.isemailon_off = status; db.SaveChanges(); } return new JsonResult { }; } }</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;_Button_Text_CheckBox_Check_MVC.EmailSubscription&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; $(document).ready(function () { $(&#39;#btnturnemailonoff&#39;).on(&#39;click&#39;, function () { var checked = $(&#34;#Chkemailonoff&#34;).prop(&#39;checked&#39;); var message = checked ? &#39;Turn Email On&#39; : &#39;Turn Email Off&#39;; if (confirm(&#34;Do you want to &#34; + message + &#34;? &#34;)) { $(&#34;#Chkemailonoff&#34;).prop(&#39;checked&#39;, !checked); $(this).val(message) $.ajax({ url: &#39;Home/CheckEmail&#39;, type: &#34;POST&#34;, data: { checkemails: !checked }, dataType: &#34;json&#34;, success: function () { //alert(&#34;ajax request to server succeed&#34;); } }); } //end of if }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;%if (Model.isemailon_off == 0) {%&gt; &lt;input type=&#34;button&#34; value=&#34;Turn Email On&#34; id=&#34;btnturnemailonoff&#34; /&gt; &lt;input type=&#34;checkbox&#34; id=&#34;Chkemailonoff&#34; /&gt; &lt;%} else {%&gt; &lt;input type=&#34;button&#34; value=&#34;Turn Email Off&#34; id=&#34;btnturnemailonoff&#34; /&gt; &lt;input type=&#34;checkbox&#34; id=&#34;Chkemailonoff&#34; checked /&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/867793/Change-Button-Text-and-CheckBox-checked-unchecked-based-on-Database-value-in-ASPNet-MVC/https://www.aspforums.net:443/Threads/867793/Change-Button-Text-and-CheckBox-checked-unchecked-based-on-Database-value-in-ASPNet-MVC/Thu, 11 Jul 2019 03:25:26 GMT