0

How can I pass value of PGId by click checkbox and get value of checkbox to update value.

my view display a list got from method "Index"

        @model PagedList.IPagedList<PG.Admin.Models.PGProfiles.PGProfileViewModel>
        @using (Html.BeginForm("UpdateStatus", "PGProfile", FormMethod.Post))
        {
            for (int i = 0; i < Model.Count; i++)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(m => m[i].PGId)
                        @Html.HiddenFor(m => m[i].PGId)
                    </td>
                    <td>
                        @Html.CheckBoxFor(m => m[i].Status)
                        <button type="submit" class="btn btn-primary btn-sm">update</button>
                    </td>
                </tr>
            }
        <script>
         // how can I write a ajax event of checkbox to pass value of PGId to controller
        </script

my controller nested method Index to get all data and method Post UpdateStatus

    // bind all data and work fine
    public ActionResult Index()
    {
      var pgProfiles = _pgProfileService.GetAllPGProfiles().ToListViewModel();
      return View(pgProfiles)
    }

    //update status by pgID
    [HttpPost]
     public ActionResult UpdateStatus(IEnumerable<PGProfileViewModel> model)
    {
        foreach (var item in model)
        {
            var pgProfiles = _pgProfileService.GetPGProfileById(item.PGId);
            pgProfiles = item.ToEntity(pgProfiles);

            pgProfiles.PGId = item.PGId;
            _pgProfileService.UpdatePGProfile(pgProfiles);
        }
        return RedirectToAction("Index");
    }
Khiem Nguyen
  • 403
  • 1
  • 8
  • 25

1 Answers1

4

If you only want to update one at a time, then you controller method need to have parameters for the objects ID and Status properties

[HttpPost]
public JsonResult UpdateStatus(int id, bool status)

and then in the view (note that CheckBoxFor() generates an checkbox with value="True" and an hidden input with value="false" which is not neccessary in your case)

@foreach (var item in Model)
{
  <tr>
    <td>
      @Html.DisplayFor(modelItem => item.PGId)
    </td>
    <td>
      if(item.Status) {
        <input type="checkbox" class="status" value=@item.PGId checked="checked />
      } else {
        <input type="checkbox" class="status" value=@item.PGId />
      }
    </td>
  </tr>
}

Script

var url = '@Url.Action("UpdateStatus", "YourControllerName")';
$('.status').click(function() {
  var id = $(this).val();
  var status = $(this).is(':checked') ? 'true' : "false";
  $.post(url, { id: id, status: status }, function(data) {
    // do something with any data you return?
  });
});

Alternatively to avoid multiple calls, you can just submit the form and bind the collection (no javascript and one call after the user has made all required changes)

@model IList<PGProfileViewModel>
@using(Html.BeginForm())
{
  for(int i = 0; i < Model.Count; i++)
  {
    <tr>
      <td>
        @Html.DisplayFor(m => m[i].PGId)
      </td>
      <td>
        @Html.HiddenFor(m => m[i].PGId)
        @Html.CheckBoxFor(m => m[i].Status)
      </td>
    </tr>
  }
  <input type="submit" />
}

and post back to

[HttpPost]
public ActionResult UpdateStatus(IEnumerable<PGProfileViewModel> model)
Khiem Nguyen
  • 403
  • 1
  • 8
  • 25
  • I have updated my controller but I get a error: " Object reference not set to an instance of an object " => for (int i = 0; i < Model.Count; i++) – Khiem Nguyen Jan 19 '15 at 10:51
  • Are you populating the collection correctly in the GET method - looks like the model is null? (you have not posted your GET method) –  Jan 19 '15 at 10:54
  • Not entirely sure I understand you comment, but you need something like `public ActionResult UpdateStatus() { List model = // populate something here; return View(model); }` –  Jan 20 '15 at 04:35
  • Just noticed your update - your `Index()` method should be fine. Are you sure it is actually populating `pgProfiles` with valid data? –  Jan 20 '15 at 04:38
  • I have update latest code in controller and view for you – Khiem Nguyen Jan 20 '15 at 04:58
  • Both the `Index()` and `UpdateStatus()` method seem to be doing exactly the same thing (so not sure why you need both). Unfortunately you have deleted almost all the original code (instead of appending the new code you have tried) so this question and my answer don't really make sense anymore. Note also if you use a `for` loop in the view, then you need to return `IList`, not `IEnumerable` –  Jan 20 '15 at 05:07
  • Hi @Stephen Muecke I found reason why It return model is null. Inside method UpdateStatus I must return RedirectToAction("Index") => work fine. May you say with me why use RedirectToAction but don't use return View("Index"). this is link resolved my issue http://stackoverflow.com/questions/25742953/mvc-nullreferenceexception-on-foreach-var-item-in-model – Khiem Nguyen Jan 20 '15 at 06:59