0 Replies - 752 Views - Last Post: 05 April 2019 - 08:07 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 219
  • Joined: 01-July 12

Wondering how should I pass roles to a view without @model or viewbag

Posted 05 April 2019 - 08:07 PM

I have a quick question just wondering whats the best way to do this. I normally use viewbag but is there a different way I should be doing this because I heard viewbag isnít a good thing to do often.
I get a user by their username and then I get the list of their roles with an [httppost]. How should I pass the roles back to the view? I already have a model of a user inserted into the view @model so I cant insert it like that return view(). What should I be doing for this?
<div class="panel panel-primary">
            <div class="panel-heading text-center">
                <h4>Get Role for Selected User</h4>
            <div class="panel-body">
                @using(Html.BeginForm("GetUserRole", "Admin"))
                        User Name: @Html.DropDownList("username",
                                   ViewBag.Users, "Select ...")
                        <input type="submit" value="Get User Role" 
                               class="btn btn-info" />
                @if(ViewBag.RolesForUser != null)
                    <div class="alert alert-info">
                        <strong>Role for User</strong>
                            @foreach(string str in ViewBag.RolesForUser)
</div>                                                                           [HttpPost]
        public async Task<ActionResult> GetUserRole(string username)
            if (!string.IsNullOrWhiteSpace(username))
                var user = await UserManager.FindByNameAsync(username);
                var roles = await UserManager.GetRolesAsync(user.Id);
                // ?? ?? or a different way
                ViewBag.RolesForUser = roles;
            return RedirectToAction("RoleManager", "Admin");

Is This A Good Question/Topic? 0
  • +

Page 1 of 1