Hi nabilabolo,
Check this example. Now please take its reference and correct your code.
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);
}
public ActionResult Save()
{
DetailModel detailmodel = new DetailModel();
detailmodel.platforms = db.platforms.Where(x => x.isSelected == true).ToList();
return View(detailmodel);
}
[HttpPost]
public ActionResult Save(DetailModel detailmodel)
{
var a = detailmodel.platforms.Where(x => x.isSelected == true).ToList();
foreach (var item in a)
{
try
{
if (ModelState.IsValid)
{
// Update the platform table.
platform p = db.platforms.Where(x => x.platform_id == item.platform_id).FirstOrDefault();
p.isSelected = false;
// Add the record to tblPlatformList table.
tblPlatformList platformList = new tblPlatformList
{
ph_no = item.ph_no,
platform_name = item.platform_name,
baseType = item.baseType,
isSelected = true
};
db.tblPlatformLists.Add(platformList);
// Save changes.
db.SaveChanges();
}
}
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("AddPlatform");
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Add_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>Save</title>
</head>
<body>
<div>
<%using (Html.BeginForm("Save", "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.TextBoxFor(m => m.platforms[i].ph_no)%>
</td>
<td class="text-center">
<%:Html.TextBoxFor(m => m.platforms[i].platform_name)%>
</td>
<td class="text-center">
<%:Html.TextBoxFor(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="Save" class="btn btn-success" />
</div>
<%}%>
</div>
</body>
</html>