c# - 如何从模型中的 IList<model> 获取 Id?

标签 c# asp.net-mvc razor

有没有一种简单的方法可以从另一个模型中的 IList 获取 id?最好使用 Razor ?我想在 IList Roles 中获取 RoleId。

    public class EditUserViewModel
    {

        public EditUserViewModel()
        {
            Claims = new List<string>(); Roles = new List<string>();
        }

        public string Id { get; set; }

        [Required]
        public string UserName { get; set; }

        [Required]
        [EmailAddress]
        public string Email { get; set; }

        public string City { get; set; }

        public List<string> Claims { get; set; }

        public IList<string> Roles { get; set; }

    }
}


   public class ManageUserRoleViewModel
    {

            public string RoleId { get; set; }
            public string RoleName { get; set; }
            public bool IsSelected { get; set; }
            //Viewbag is used to store UserId

    }

   public class UserRoleViewModel
    {
        public string UserId { get; set; }
        public string UserName { get; set; }
        public bool IsSelected { get; set; }
        //Viewbag is used to store UserId

    }
  <table class="table table-hover table-md ">

                                <thead>
                                    <tr>
                                        <td class="text-left TableHead">Role</td>
                                        <td class="text-right TableHead">Delete</td>

                                    </tr>
                                </thead>

                                @*--Table Body For Each to pull DB records--*@
                                <tbody>
                                    @foreach (var role in Model.Roles)
                                    {
                                        <tr>
                                            <td>@role</td>
                                            <td>
                                                <button class="sqButton btnRed float-right zIndex" id="Delete" title="Delete" data-toggle="ajax-modal" data-target="#deleteRoleUser" data-url="@Url.Action("Delete", "Administration", new {Id = Model.Id , Type = "roleUser"})">
                                                    <i class="glyphicon glyphicon-remove"></i>
                                                </button>
                                            </td>

                                        </tr>
                                    }
                                </tbody>

                            </table>

我正在尝试将角色 ID 与@Url.Action 中的其他参数一起传递,但我似乎无法弄清楚将其引入的 secret ,因此我可以将其传递给后端 Controller 。

最佳答案

问题是

public IList<string> Roles { get; set; }

仅包含字符串,因此没有要查找的 ID。您必须将此行更改为

public IList<ManageUserRoleViewModel> Roles { get; set; }

这样,您就有了一个包含 ID 的对象列表。然后,在您看来,您可以这样做:

@Model.Roles.FirstOrDefault(x => x.RoleId == YOUR_UNIQUE_ID)

这会给你一个对象来执行进一步的逻辑。

关于c# - 如何从模型中的 IList<model> 获取 Id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58270426/

相关文章:

c# - 在 ServiceStack.OrmLite 中更改表的别名运行时

c# - 我们如何有效地从 C# 中的文本文件中只读取整数

c# - 对 ActionResult 进行单元测试

javascript - post done 函数中的 jQuery post

asp.net-mvc - 跨项目共享 Razor View

c# - Entity Framework 和业务对象

c# - HTTP 错误 404.0 - 在 Mvc 6 Asp.net 5 中找不到

asp.net-mvc - 可以在多个ASP.net MVC项目中重用ASP.Net MVC View (* .aspx)吗?

asp.net-mvc-3 - 不同命名空间中的 Asp.Net MVC 3 Custom WebViewPage 会破坏 IntelliSense

javascript - 使用 Ajax.BeginForm 更新表单