I have a problem with sending checked checkboxes values to my controller, I checked them all but nothing happen. I can do that only one by one, and when I reload page it's been a saved, but I am not sure how to pass the values of multiple checked checkboxes back to the controller.
This is my cshtml.
<table class="table table-striped grid-table">
<tr>
<th>Samp</th>
<th>Id of Book</th>
<th>
<input type="checkbox" id="box" name="checkAll" />
</th>
</tr>
@foreach (var item in (IEnumerable<cit.Models.getCheIdTip_Result>)Model)
{
<tr>
<td>@item.idtip</td>
<td>@item.tipname</td>
<td>
<div class="pure-checkbox">
<input type="checkbox" idtip="@item.idtip" class="checktip" checked="@(item.idemployee == ViewBag.idemployee ? true : false)" name="@item.id.ToString()" id="@item.id.ToString()" />
<label for="@item.id.ToString()"></label>
</div>
</td>
</tr>
}
</table>
<input type="hidden" value="@ViewData["idemployee"]" name="idemployee" id="idemployee" class="idemployee" />
</div>
This is my controller
public JsonResult CheckUsers(int idemployee, int idtip, bool che)
{
try
{
if (che)
{
checkusers cheuser = new checkusers();
cheuser.idemployee = idemployee;
cheuser.idtip = idtip;
Database.checkusers.Add(cheuser);
Database.SaveChanges();
}
else if (!che)
{
var cheuserdelete = Database.checkusers.Where(a => a.idemployee == idemployee && a.idtip == idtip).FirstOrDefault();
Database.checkusers.Remove(cheuserdelete);
Database.SaveChanges();
}
if (Request.IsAjaxRequest())
{
return Json(true, JsonRequestBehavior.AllowGet);
}
else
{
return Json(true, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
Logger.Error("Error: {0}", ex.ToString());
return null;
}
}
And this is my jquery with post method.
$('#box').click(function () {
$('.checktip').prop('checked', true);
var idemployee = $('.idemployee').val();
var idtip = $(this).attr('idtip');
var che = $(this).prop('checked');
$.ajax({
url: UrlSettingsDocument.EmpTipes,
data: { idemployee: idemployee, idtip: idtip, che: che },
type: "POST",
success: function (result) {
if (result.result == "Redirect") {
window.location = result.url;
}
}
});
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…