Hi nabilabolo,
Check this example. Now please take its reference and correct your code.
SQL
CREATE TABLE platform
(
platform_id INT IDENTITY PRIMARY KEY,
platform_name VARCHAR(50),
ph_no VARCHAR(50),
baseType VARCHAR(50),
isSelected BIT NOT NULL,
EmailDate DATETIME
)
INSERT INTO platform VALUES('P1','1111','B1',1,GETDATE())
INSERT INTO platform VALUES('P2','2222','B2',0,GETDATE())
INSERT INTO platform VALUES('P3','3333','B3',1,GETDATE())
INSERT INTO platform VALUES('P4','4444','B4',1,GETDATE())
SELECT * FROM platform
Model
public class DetailModel
{
public List<platform> platforms { get; set; }
}
Controller
public class HomeController : Controller
{
PlatformEntities db = new PlatformEntities();
public ActionResult Index()
{
DetailModel detailmodel = new DetailModel();
detailmodel.platforms = db.platforms.Where(x => x.isSelected == true).ToList();
return View(detailmodel);
}
[HttpPost]
public ActionResult EmailedPlatform(DetailModel detailmodel)
{
var selectedPlatform = detailmodel.platforms.Where(x => x.isSelected == false).ToList();
foreach (var item in selectedPlatform)
{
try
{
if (ModelState.IsValid)
{
platform p = db.platforms.Where(x => x.platform_id == item.platform_id).FirstOrDefault();
p.isSelected = false;
p.EmailDate = null;
db.SaveChanges();
}
else
{
ModelState.AddModelError("", "Update Fail");
}
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
System.Diagnostics.Debug.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
}
}
}
}
return RedirectToAction("Index", "Home");
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<_174367_Update_CheckBox_MVC.Models.DetailModel>" %>
<!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>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body>
<div>
<%using (Html.BeginForm("EmailedPlatform", "Home", FormMethod.Post))
{ %>
<div class="table" id="table_id">
<table id="ItemList" class="table table-striped table-bordered" style="width: 100%">
<thead>
<tr>
<th class="text-center">PH Number</th>
<th class="text-center">Platform Name</th>
<th class="text-center">Base Type</th>
<th class="text-center">Email Sent</th>
</tr>
</thead>
<tbody>
<%for (int i = 0; i < Model.platforms.Count; i++)
{ %>
<tr>
<td class="text-center">
<%:Html.DisplayFor(m => m.platforms[i].ph_no)%>
</td>
<td class="text-center">
<%:Html.DisplayFor(m => m.platforms[i].platform_name)%>
</td>
<td class="text-center">
<%:Html.DisplayFor(m => m.platforms[i].baseType)%>
</td>
<td class="text-center">
<%:Html.HiddenFor(m => m.platforms[i].platform_id) %>
<%:Html.HiddenFor(m => m.platforms[i].ph_no)%>
<%:Html.HiddenFor(m => m.platforms[i].platform_name) %>
<%:Html.HiddenFor(m => m.platforms[i].baseType)%>
<%:Html.CheckBoxFor(m => m.platforms[i].isSelected)%>
</td>
</tr>
<%}%>
</tbody>
</table>
<br />
<input type="submit" value="Send Email" class="btn btn-success" />
</div>
<%}%>
</div>
</body>
</html>
Screenshots
Database record after update