简体   繁体   English

将对象列表传递给 Controller

[英]Passing List of Objects to Controller

I have a ViewModel named CreateRoleViewModel which contains the items shown below.我有一个名为 CreateRoleViewModel 的 ViewModel,其中包含如下所示的项目。 On the view, I'm iterating through a list of permissions and displaying them.在视图中,我正在遍历权限列表并显示它们。 What I'm trying to accomplish is to allow a user to create a new role and add permissions to that role.我想要完成的是允许用户创建一个新角色并向该角色添加权限。 I've been able to successfully pass all data to the controller except the permissions.除了权限之外,我已经能够成功地将所有数据传递给 controller。 Hopefully I'm not confusing anyone.希望我不会让任何人感到困惑。 I've tried to use a for loop (which did't produce correct results) and a foreach loop, which did produce the correct results (meaning displayed all permissions currently available).我尝试使用 for 循环(它没有产生正确的结果)和一个 foreach 循环,它确实产生了正确的结果(意味着显示当前可用的所有权限)。 I realize I'm going to also, most likely need to update my domain model for Permissions to include columns for View, Modify etc.我意识到我也要去,很可能需要更新我的域 model 的权限以包括查看、修改等列。

CreateViewModel创建视图模型

public class CreateRoleViewModel
{
    [Required]
    public string RoleName { get; set; }
    public string RoleDescription { get; set; }
    public List<Permissions> Permissions { get; set; }
    public bool AllowViewAccess { get; set; }
    public bool AllowModifyAccess { get; set; }
    public CreateRoleViewModel()
    {
        Permissions = new List<Permissions>();
    }
}

PermissionsController权限控制器

[Route("Administration/Permissions/CreateRole")]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult CreateRole(CreateRoleViewModel newRole)
    {
        try
        {
            var test = newRole;
            return RedirectToAction(nameof(Index));
        }
        catch
        {
            return View();
        }
    }

View看法

<form method="post" asp-action="CreateRole">

        <div class="form-group col-6" style="padding-left:0">
            <label asp-for="RoleName"></label>
            <input asp-for="RoleName" class="form-control" />
            <span asp-validation-for="RoleName" class="text-danger"></span>
        </div>
        <div class="form-group col-6" style="padding-left:0">
            <label asp-for="RoleDescription"></label>
            <input asp-for="RoleDescription" class="form-control" />                
        </div>
        <div class="form-group ">
            <h4>Permissions</h4>
            <div class="card">
                <div class="card-header">
                    <input type="checkbox" class="form-check-input p-2" id="selectAll">
                    <label class="form-check-label" for="selectAll">Select All<span style="font-weight:bold;font-style:italic;"> (This will allow read/write access to ALL selected items.)</span></label>
                </div>
            </div>
            <!--Permissions should go here-->
            <div class="card-body border">
                @foreach (var item in Model.Permissions)
                {
                <div class="row" style="border-radius:3px;border-top:2px solid gray;border-bottom:2px solid gray;padding:15px;">
                    <div class="col-8">
                        <input type="checkbox" class="form-check-input p-2" id="select">
                        <label class="form-check-label" for="select" style="font-weight:bold;">@item.PermissionName</label>
                        <p style="color:gray;">@item.PermissionDescription</p>
                    </div>
                    <div class="col-2">
                        <input type="checkbox" class="form-check-input p-2" id="readOnly" asp-for="AllowViewAccess">
                        <label class="form-check-label" for="readOnly">View</label>
                    </div>
                    <div class="col-2">
                        <input type="checkbox" class="form-check-input p-2" id="readWrite" asp-for="AllowModifyAccess">
                        <label class="form-check-label" for="readWrite">Modify</label>
                    </div>
                </div>
                }


            </div>
            <div asp-validation-summary="All" class="text-info"></div>
            <button style="background-color: #6987D5;color:white;" class="btn">Save</button>
            <button class="btn" style="background-color:lightgray;border:1px solid darkgray">Cancel</button>
        </div>
    </form>

From your description and the view code, I think you should put the "AllowViewAccess " and "AllowModifyAccess " in the Permissions class, because the role have separate "View" or "Modify" access to different permissions.根据您的描述和查看代码,我认为您应该将“AllowViewAccess”和“AllowModifyAccess”放在 Permissions class 中,因为角色对不同的权限具有单独的“查看”或“修改”访问权限。 Based on your codes, I made an example:根据您的代码,我举了一个例子:

Model: Model:

public class CreateRoleViewModel
{
    [Required]
    public string RoleName { get; set; }
    public string RoleDescription { get; set; }
    public List<Permissions> Permissions { get; set; }

    public CreateRoleViewModel()
    {
        Permissions = new List<Permissions>();
    }
}

public class Permissions
{
    public string PermissionName { get; set; }
    public string PermissionDescription { get; set; }
    public bool AllowViewAccess { get; set; }
    public bool AllowModifyAccess { get; set; }
}

View:看法:

@model CreateRoleViewModel

@{ 
    var i = 0;
}

<form method="post" asp-action="CreateRole">

    <div class="form-group col-6" style="padding-left:0">
        <label asp-for="RoleName"></label>
        <input asp-for="RoleName" class="form-control" />
        <span asp-validation-for="RoleName" class="text-danger"></span>
    </div>
    <div class="form-group col-6" style="padding-left:0">
        <label asp-for="RoleDescription"></label>
        <input asp-for="RoleDescription" class="form-control" />
    </div>
    <div class="form-group ">
        <h4>Permissions</h4>
        <div class="card">
            <div class="card-header">
                <input type="checkbox" class="form-check-input p-2" id="selectAll">
                <label class="form-check-label" for="selectAll">Select All<span style="font-weight:bold;font-style:italic;"> (This will allow read/write access to ALL selected items.)</span></label>
            </div>
        </div>
        <!--Permissions should go here-->
        <div class="card-body border">
            @foreach (var item in Model.Permissions)
            {
                <div class="row" style="border-radius:3px;border-top:2px solid gray;border-bottom:2px solid gray;padding:15px;">
                    <div class="col-8">
                        <input type="checkbox" class="form-check-input p-2" id="select">
                        <input type="hidden" asp-for="@Model.Permissions[i].PermissionName">
                        <label class="form-check-label" for="select" style="font-weight:bold;">@item.PermissionName</label>
                        <p style="color:gray;">@item.PermissionDescription</p>
                        <input type="hidden" asp-for="@Model.Permissions[i].PermissionDescription">
                    </div>
                    <div class="col-2">
                        <input type="checkbox" class="form-check-input p-2" id="readOnly" asp-for="@Model.Permissions[i].AllowViewAccess">
                        <label class="form-check-label" for="readOnly">View</label>
                    </div>
                    <div class="col-2">
                        <input type="checkbox" class="form-check-input p-2" id="readWrite" asp-for="@Model.Permissions[i].AllowModifyAccess">
                        <label class="form-check-label" for="readWrite">Modify</label>
                    </div>
                </div>
                i++;
            }


        </div>
        <div asp-validation-summary="All" class="text-info"></div>
        <button style="background-color: #6987D5;color:white;" class="btn">Save</button>
        <button class="btn" style="background-color:lightgray;border:1px solid darkgray">Cancel</button>
    </div>
</form>

Controller: Controller:

public IActionResult Index()
{
    CreateRoleViewModel createRoleViewModel = new CreateRoleViewModel();
    return View(createRoleViewModel);
}

//[Route("Administration/Permissions/CreateRole")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CreateRole(CreateRoleViewModel newRole)
{
    try
    {
        var test = newRole;
        return RedirectToAction(nameof(Index));
    }
    catch
    {
        return View();
    }
}

Result:结果:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过查询字符串将对象列表传递给 MVC Controller - Passing List of objects via querystring to MVC Controller 使用ajax调用在控制器中传递对象列表 - passing list of objects in controller using ajax call 将复杂的javascript对象传递给控制器​​。 对象列表始终为0 - Passing complex javascript object to controller. List of objects are always 0 使用 jQuery Ajax 将对象列表传递给 MVC 控制器方法 - Passing A List Of Objects Into An MVC Controller Method Using jQuery Ajax 使用jQuery Ajax将对象列表传递到ActionResult MVC控制器方法 - Passing A List Of Objects Into An ActionResult MVC Controller Method Using jQuery Ajax 为什么将带有子对象列表的模型传递给控制器​​时,子对象列表没有通过? - Why When passing a Model, with a list of child objects, to a Controller the list of child objects is not passed through? 将列表集合传递给控制器 - passing list collection to controller 在AuthorizeAttribute和Controller之间传递对象 - Passing objects between AuthorizeAttribute and Controller 将 JavaScript 对象数组传递给控制器 - Passing JavaScript Array of Objects to Controller 使用 Html.BeginForm() ASP.NET MVC 将对象列表从控制器传递到视图时出错 - Error when passing list of objects from Controller to View using Html.BeginForm() ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM