【问题标题】:how do i get all already selected checkbox value in edit page in mvc using jquery如何使用 jquery 在 mvc 的编辑页面中获取所有已选择的复选框值
【发布时间】:2014-12-08 19:57:44
【问题描述】:

我有一个编辑页面,用户必须在其中选择复选框,然后将其选择的值提交到正在工作的数据库,但是有一个问题我必须捕获在创建时间。我如何在编辑页面上获得已选中的复选框值。 这是我的提交代码。

$(document).ready(function() {
    $('#btn-gn').click(function () {
        var list = [];
        $('#MySelection input:checked').each(function () {
            list.push(this.name);
        });
        // now names contains all of the names of checked checkboxes
        // do something with it for excamle post with ajax
        $.ajax({
            url: '@Url.Action("RoleEdit","Role")',
            type: 'POST',
            data: { Parameters: list},
            success: function (result) {
                alert("success");
            },
            error: function (result) {
                alert("error!");
            }
        });   //end ajax
    });
});



 <div id="MySelection">
        @for (int i = 0; i < Model.PermissionsList.Count; i++)
            {
        <div class="row">
        <div class="col-md-1 col-sm-1">

           <input type="checkbox" name="tags" class="no-margin"
            id="=ids" value="@Model.PermissionsList[i].PermissionId" >

       </div>
        <div class="col-md-11 col-sm-11">
              @Model.PermissionsList[i].PermissionName

       </div>
        </div>
            }
        </div>

这是我的控制器:

[HttpGet]
        public ActionResult RoleEdit(int id,ViewModelRole viewModelRole)
        {

             viewModelRole.Role = roleService.GetRole(id);
             viewModelRole.PermissionsList = preFlightDbContext.Permissions.ToList();
            return View(viewModelRole);
        }

        [HttpPost]
        public ActionResult RoleEdit(ViewModelRole viewModelRoleForAdd, string[] tags)
        {
            string addPer = delemetedString(tags, ',');
            try
            {
                if (ModelState.IsValid)
                {

                   viewModelRoleForAdd.Role= roleService.UpdateRole(role);
                    roleService.SaveRole();
                    return RedirectToAction("RoleLists");
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, " +
                  "and if the problem persists see your system administrator.");
            }
            return View(viewModelRoleForAdd);
        }



private string delemetedString(string[] input, char delemeter)
        {
            string output = String.Empty;
            for (int i = 0; i < input.Length; i++)
            {
                if (i == input.Length - 1)
                    output += input[i];
                else
                    output += input[i] + delemeter;
            }
            return output;
        }

【问题讨论】:

  • 你能添加你的控制器类吗?到目前为止,代码看起来正确
  • 好的,我正在更新我的代码
  • 您可以在复选框上添加 checkchange 事件,并保留已更改名称的列表。
  • 您想要复选框的名称吗?还是价值观?
  • @Stephen Muecke 我希望复选框出现在带有选中复选框的编辑页面中

标签: jquery asp.net-mvc checkbox


【解决方案1】:

我会建议这样的事情:

public class PermissionsListItem()
{
   public int Id {get; set;}
   public string Name {get; set;}
   public bool isChecked {get; set;}
}

现在控制器动作方法:

public ViewResult Index()
{
   ...code to build up a List<PermisionListItem>()

   return View(List<PermissionListItem> model goes here);
}

[POST]
public ActionResult Edit(List<PermissionListItem> items)
{
   iterate through your model to see what is checked.
}

剃刀视图

@model IEnumerable<PermissionListItem>

<div id="MySelection">
@using(Html.BeginForm())
{  
    @for (var item in model)
    {
        <div class="row">
        <div class="col-md-1 col-sm-1">
            @Html.CheckBoxFor(x => @item.IsSelected, new { name="tags", @class="no-margin"})

       </div>
       <div class="col-md-11 col-sm-11">
           @item.Name
       </div>
   </div>
   }
</div>
}

【讨论】:

  • 能否举个完整的例子
  • 我正在尝试得到一些错误 viewModelRole.Permiss = preFlightDbContext.Permissions .Select(p => new List() { Id = p.PermissionId, Name = p.PermissionName });
猜你喜欢
  • 1970-01-01
  • 2013-01-06
  • 2015-07-05
  • 2020-01-16
  • 2020-11-23
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多