[英]Mvc Ajax post from check-boxes in partial view
I have a partial view which is used to show a checkbox in a column of table in razor. 我有一个局部视图,用于在剃刀中的表格列中显示一个复选框。 When I click on any checkbox in the table its posting back to controller with Id of first row and not the Id of row in which the check-box is contained.
当我单击表中的任何复选框时,其回发到控制器的第一行ID而不是包含复选框的行ID。 The javascript function to post ajax request is "SaveChanges" as given below.
发出ajax请求的javascript函数为“ SaveChanges”,如下所示。 The hidden field "RecurrenceScheduleId" is the primary key id.
隐藏的字段“ RecurrenceScheduleId”是主键ID。
Am I doing something wrong here? 我在这里做错什么了吗?
- Following are my view & controller action: -以下是我的视图和控制器操作:
@model SMRDS.Model.RecurrenceSchedule
@using (Ajax.BeginForm("_LogOccurancePartial", "Schedule",
new AjaxOptions
{
UpdateTargetId = "tdOccurance",
HttpMethod = "post",
LoadingElementId = "btnProgress",
OnBegin = "dialogBegin",
OnSuccess = "updateSuccess",
OnFailure = "dialogFailure"
},
new { id = "frm-toggle" }))
{
@Html.HiddenFor(model => model.RecurrenceScheduleId)
@Html.CheckBoxFor(m => m.LogOccurences, new { @onclick ="SaveChanges(this)" })
}
<script>
function updateSuccess() {}
function dialogFailure() {}
function dialogComplete() {}
function dialogBegin() {}
function SaveChanges(checkboxInput) {
$.ajax({
type: 'POST',
url: '/Schedule/_LogOccurancePartial',
data: { newValue: checkboxInput.checked, id: $("#RecurrenceScheduleId").val() },
dataType: 'json',
success: function (response) {
//handle success
}
});
}
Controller Action : 控制器动作:
public JsonResult _LogOccurancePartial(bool newValue,int id)
{
var result = BLL.Service.RecurrenceSchedules.UpdateLogOccurence(id, newValue);
return Json(new { result = result }, JsonRequestBehavior.AllowGet);
}
Update 更新资料
Following is the html rendered for hidden fields. 以下是为隐藏字段呈现的html。 At present I have only two rows with Ids "40" & "41".
目前,我只有两行,其ID为“ 40”和“ 41”。
<input data-val="true" id="RecurrenceScheduleId"
name="RecurrenceScheduleId" type="hidden" value="40">
<input data-val="true" id="RecurrenceScheduleId"
name="RecurrenceScheduleId" type="hidden" value="41">
It is your responsibility to maintain unique ids when you use the helpers. 使用助手时,您有责任维护唯一的ID。
If you had a model 如果你有模特
public class ViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
And your view contains a collection you can use a for loop with an index to override the id with unique values. 并且您的视图包含一个集合,您可以使用带索引的for循环使用唯一值覆盖id。
@model List<ViewModel>
@for(int i = 0; i < Model.Count(); i++)
{
@Html.HiddenFor(m => Model[i].Id, htmlAttributes: new { id = "item" + item.Id })
}
But you can also adjust your click handler so you don't need to find a hidden input by id. 但是,您也可以调整点击处理程序,因此您无需按ID查找隐藏的输入。
@Html.CheckBoxFor(m => m.LogOccurences,
new { @class="trigger", data_rsid=m.RecurrenceScheduleId })
<script>
$("body").on("click", ".trigger", function(e) {
var rsId = $(this).data("rsid");
var checked = $(this).prop("checked");
var data = { newValue: checked, id: rsId }
$.ajax({ ... });
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.