mardi 28 juin 2016

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. The javascript function to post ajax request is "SaveChanges" as given below. The hidden field "RecurrenceScheduleId" is the primary key 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. At present I have only two rows with Ids "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">

Aucun commentaire:

Enregistrer un commentaire