zoukankan      html  css  js  c++  java
  • 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的理解.

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

  • 相关阅读:
    java监听器之实现在线人数显示
    java之web开发过滤器
    java之MVC开发模式
    java之jsp内置对象
    java之jsp页面语法
    java之jsp实现动态网页
    java数据库(MySQL)之增删改查
    java数据库之JDBC
    java线程之线程通信控制
    java线程之线程通信
  • 原文地址:https://www.cnblogs.com/cheesebar/p/5669307.html
Copyright © 2011-2022 走看看