Hi chetan,
Check this example. Now please take its reference and correct your code.
Database
CREATE TABLE EmailSubscription(Id INT IDENTITY PRIMARY KEY,isemailon_off INT)
INSERT INTO EmailSubscription VALUES(0)
Model
public class CheckUnCheckModel
{
public string ButtonText { get; set; }
public bool CheckUnCheck { get; set; }
}
Controller
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) ? "Turn Email Off" : "Turn Email On";
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() == "turn email on" ? 1 : 0;
entities.SaveChanges();
CheckUnCheckModel model = BindCheckBoxButton();
return View(model);
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CheckUnCheckModel>" %>
<%@ Import Namespace="_834192_Check_UnCheck_CheckBox.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("Index", "Home", FormMethod.Post))
{ %>
<input type="submit" value='<%= Model.ButtonText%>' name="submit" />
<input name="CheckUnCheck" <%=(Convert.ToBoolean(Model.CheckUnCheck) ? "checked='checked'" : string.Empty)%>
type="checkbox" />
<%} %>
</body>
</html>
Screenshot