This must be the easy one but I've been exploring for some days about how to change a row value clicking on a CheckBox. What I want is to change the value of a column clicking on the CheckBox and keep the CheckBox checked if it is true. Otherwise it will remain unchecked if false. Below is a screenshot and when Public Post column value of a row is 'No', then the CheckBox will remain unchecked. And when I check the CheckBox, then vice-verse means it will remain checked and value will be 'Yes'.
I am not sure how to do it exactly and seen some of the problems or links that deals with this situation:
ASP.NET MVC submitting form on checkbox click
How can I get CheckBox to maintain checked state across postback in ASP.NET MVC?
But I would like to have some more advanced ideas to implement it as it is totally different from ASP.NET Web Form. I would appreciate if experts share some ideas or helpful links to understand. Thanks.
By the way, following is the class that I am working with. I am not sure how to handle the postback from controller and so far following is done using Ajax. But it is not working or updating:
Model:
public class Product
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "Details can't be blank")]
[DataType(DataType.Text)]
public string Name{ get; set; }
public string Date_Posted { get; set; }
public string Time_Posted { get; set; }
public string Time_Edited { get; set; }
public string Date_Edited { get; set; }
public string Public { get; set; }
public double Price { get; set; }
public int User_Id { get; set; }
public bool Status { get; set; } //This is bit column that I am trying to change and not sure how to handle this from controller
public List<Product> allLists;
public IEnumerable<Product> Items;
public Pager Pager;
}
Controller - HomeController.cs:
[HttpPost]
public ActionResult CheckValue(int id)
{
MainDbContext db = new MainDbContext();
List result = db.Product.Find(id);
if (ModelState.IsValid)
{
result.Public = "Yes";
result.Status = true;
db.Entry(result).State = EntityState.Modified;
db.SaveChanges();
}
return View(db.Lists.ToList());
}
View - Home/Index:
@model OurFirstWebApp.Models.Product
@{
ViewBag.Title = "Index";
var username = User.Identity.Name;
}
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script language="javascript">
$(function () {
$('.toggle').change(function () {
//alert("Hello");
var self = $(this);
var url = self.data('url');
var id = self.attr('id');
//var value = self.prop('checked');
$.ajax({
url: url,
data: { id: id },
type: 'POST',
success: function (response) {
alert(response);
$("#divData").load(window.location + " #divData");
}
});
});
});
<div id="divData">
<table class="table table-bordered table-condensed" id="data">
<thead>
<tr>
<th style="text-align:center;">ID</th>
<th style="text-align:center;">Products</th>
<th style="text-align:center;">Time Posted</th>
<th style="text-align:center;">Time Edited</th>
@if (@User.Identity.IsAuthenticated)
{
<th style="text-align:center;">Status</th>
<th style="text-align:center;">Edit</th>
<th style="text-align:center;">Delete</th>
<th style="text-align:center;">Public Post</th>
}
</tr>
</thead>
<tbody>
@foreach (var item in Model.Items)
{
<tr>
<td id="ID" style="text-align: center;">@Html.DisplayFor(modelItem => item.Id)</td>
<td style="text-align: center;">@Html.DisplayFor(modelItem => item.Name)</td>
<td style="text-align: center;">@Html.DisplayFor(modelItem => item.Time_Posted)</td>
<td style="text-align: center;">@Html.DisplayFor(modelItem => item.Time_Edited)</td>
@if (@User.Identity.IsAuthenticated)
{
<td style="text-align: center;">@Html.CheckBoxFor(modelItem => item.Status, new { id = item.Id, @class = "toggle", data_url = Url.Action("CheckValue", "Home") })</td>
<td style="text-align: center;"><a href="@Url.Action("Edit", "Auth")/@Html.DisplayFor(modelItem => item.Id)">Edit</a></td>
<td style="text-align: center;"><a href="@Url.Action("Delete", "Auth")/@Html.DisplayFor(modelItem => item.Id)">Delete</a></td>
<td id="chngData" style="text-align: center;">@Html.DisplayFor(modelItem => item.Public)</td>
}
</tr>
}
</tbody>
</table>