我怎样写复选框的AJAX事件立即通过值Id在行至控制器 [英] How do I write a ajax event of checkbox to pass value Id immediately on row to controller

查看:103
本文介绍了我怎样写复选框的AJAX事件立即通过值Id在行至控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能够通过点击复选框PGID传递价值和获取复选框的值更新值。

我的观点显示一个列表的方法从指数

得到

  @model PagedList.IPagedList< PG.Admin.Models.PGProfiles.PGProfileViewModel>
        @using(Html.BeginForm(UpdateStatus,PGProfile,FormMethod.Post))
        {
            的for(int i = 0; I< Model.Count;我++)
            {
                &所述; TR>
                    &所述; TD>
                        @ Html.DisplayFor(M = GT; M [​​] .PGId)
                        @ Html.HiddenFor(M = GT; M [​​] .PGId)
                    < / TD>
                    &所述; TD>
                        @ Html.CheckBoxFor(M = GT; M [​​] .STATUS)
                        <按钮式=提交级=BTN BTN-主要BTN-SM>更新< /按钮>
                    < / TD>
                < / TR>
            }
        <脚本>
         我怎么//可以写复选框的Ajax事件传递PGID的值到控制器
        < / SCRIPT

我的控制器嵌套指数的方法来获取所有数据和方法后UpdateStatus

  //绑定的所有数据,做工精细
    公众的ActionResult指数()
    {
      VAR pgProfiles = _pgProfileService.GetAllPGProfiles()ToListViewModel()。
      返回查看(pgProfiles)
    }    通过PGID //更新状态
    [HttpPost]
     公众的ActionResult UpdateStatus(IEnumerable的< PGProfileViewModel>模型)
    {
        的foreach(模型VAR项)
        {
            VAR pgProfiles = _pgProfileService.GetPGProfileById(item.PGId);
            pgProfiles = item.ToEntity(pgProfiles);            pgProfiles.PGId = item.PGId;
            _pgProfileService.UpdatePGProfile(pgProfiles);
        }
        返回RedirectToAction(「指数」);
    }


解决方案

如果你只是想在同一时间更新一个,那么你控制器方法需要有对象的参数 ID 状态属性

  [HttpPost]
公共JsonResult UpdateStatus(INT ID,布尔状态)

,然后在视图(注意, CheckBoxFor()生成带有一个复选框值=真和与隐藏的输入值=FALSE这时并不需要你的情况)

  @foreach(以型号VAR项)
{
  &所述; TR>
    &所述; TD>
      @ Html.DisplayFor(modelItem => item.PGId)
    < / TD>
    &所述; TD>
      如果(item.Status){
        <输入类型=复选框级=状态value=@item.PGId检查=选中/>
      }其他{
        <输入类型=复选框级=状态value=@item.PGId />
      }
    < / TD>
  < / TR>
}

剧本

  VAR URL ='@ Url.Action(UpdateStatus,YourControllerName)';
$('状态')。点击(函数(){
  VAR ID = $(本).VAL();
  VAR状态= $(本)。是(':检查')? '真假;
  $。员额(URL,{ID:ID,状态:状态}功能(数据){
    //做你返回任何数据的东西吗?
  });
});

另外,避免多次调用,您可以只提交表单并绑定集合(没有javascript和一个呼叫用户做出毕竟需要改变)

  @model的IList< PGProfileViewModel>
@using(Html.BeginForm())
{
  的for(int i = 0; I< Model.Count;我++)
  {
    &所述; TR>
      &所述; TD>
        @ Html.DisplayFor(M = GT; M [​​] .PGId)
      < / TD>
      &所述; TD>
        @ Html.HiddenFor(M = GT; M [​​] .PGId)
        @ Html.CheckBoxFor(M = GT; M [​​] .STATUS)
      < / TD>
    < / TR>
  }
  <输入类型=提交/>
}

和回发到

  [HttpPost]
公众的ActionResult UpdateStatus(IEnumerable的< PGProfileViewModel>模型)

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");
    }

解决方案

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)

这篇关于我怎样写复选框的AJAX事件立即通过值Id在行至控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆