view向controller提交列表

第一次将view中列表提交到controller,尝试了下,还是可以的

要传输的实体类

    /// <summary>
    /// 用于展示的角色类
    /// </summary>
    public class Action4Role
    {
        /// <summary>
        /// 行为Id
        /// </summary>
        public string ActionId { get; set; }
        /// <summary>
        /// 角色Id
        /// </summary>
        public string RoleId { get; set; }
        /// <summary>
        /// 角色名字
        /// </summary>
        public string ActionName { get; set; }
        /// <summary>
        /// 所属项目名称
        /// </summary>
        public string ApplicationName { get; set; }
        /// <summary>
        /// 是否授权
        /// </summary>
        public bool IsAuthorize { get; set; }
    }

 

get请求

 

        public ActionResult ActionRole(string RoleId)
        {
            var allActions = PowerService.GetActions().ToList();
            var role = new Role4Select() { Id = RoleId };
            var roles = PowerService.GetRoles(role).ToList();
            ViewData["RoleName"] = PowerService.GetRoleById(RoleId).Name;
            return View(roles);
        }

 

@model List<DTOs.Role.Action4Role>

@{
    ViewBag.Title = "ActionRole";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>行为角色</h2>
@using (Html.BeginForm("ActionRole", "Power", FormMethod.Get))
{
    <div class="form-group">
        <div class="col-md-10">
            <p>@ViewData["RoleName"]</p>
        </div>
    </div>
}
@using (Html.BeginForm("SaveActionRoleList", "Power", FormMethod.Post))
{
    <table class="table">
        <tr>
            <th>
                全选<input type="checkbox" id="checkAll" />
            </th>
            <th>
                行为
            </th>
            <th>
            </th>
        </tr>

        @for (int i = 0; i < Model.Count() ; i++)
        {
            @Html.HiddenFor(model => model[i].ActionId)
            @Html.HiddenFor(model => model[i].RoleId)
            @Html.HiddenFor(model => model[i].ApplicationName)
            <tr>
                <td>
                    @Html.CheckBoxFor(model => model[i].IsAuthorize)
                </td>
                <td>
                    @Html.DisplayTextFor(model => model[i].ActionName)

                </td>
            </tr>
        }
    </table>
    <div class="form-group">
        <div class="col-md-offset-2 col-md-10 pull-left">
            <input type="submit" value="保存" class="btn btn-default" />
        </div>
    </div>
}
<script src="~/Scripts/jquery-2.1.1.min.js"></script>
<script>
    $(function () {
        $('#checkAll').change(function () {
            var isAll = document.getElementById('checkAll').checked;
            $checkBoxs = $('input[type="checkbox"]').each(function () {
                $(this).prop("checked", isAll);
            })
        })
    })
</script>

 post请求

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveActionRoleList([Form]IEnumerable<Action4Role> actionList)
        {
            var roleId = actionList.First().RoleId;
            var IsAuthRoleIds = actionList.Where(a => a.IsAuthorize).Select(a => a.ActionId);
            var NoAuthRoleIds = actionList.Where(a => !a.IsAuthorize).Select(a => a.ActionId);
            PowerService.RoleAddAction(roleId, IsAuthRoleIds.ToArray());
            PowerService.RoleDeleteAction(roleId, NoAuthRoleIds.ToArray());
            return RedirectToAction("RoleList");
        }

 以前总以为Model和字典在view和controller之间的通讯没有太多区别,也通过这个需求加深了对Model的理解.

 加油,相信自己,明天可以的!

posted @ 2016-07-14 09:49  行走即歌  阅读(292)  评论(0编辑  收藏  举报


感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。本文欢迎各位转载,但是转载文章之后必须在文章页面中给出作者和原文连接