S. Kellar
S. Kellar

Reputation: 45

How to get Id from IList<model> within model?

Is there a simple way to get an id from an IList within another model? Preferably using razor? I want to get the RoleId within IList Roles.

    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>

I am trying to pass the Role Id with the other params in the @Url.Action but I can't seem to figure out the secret to pull it in, so i can pass it to the back end controller.

Upvotes: 1

Views: 832

Answers (1)

yesman
yesman

Reputation: 7848

The problem is that

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

Contains only strings, so there's no ID to look for. You would have to change this line to

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

This way, you have a list of objects that also hold an ID. Then, in your view, you can do this:

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

This will give you an object to perform further logic on.

Upvotes: 3

Related Questions