Click here to Skip to main content
15,883,787 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to get the Roles for all users like this.

C#
public ActionResult RoleIndex()
        {
            var profilesList = db.UserProfiles.Select(u => u.UserName).ToList();

            RolesAndUser r = new RolesAndUser();
            if (profilesList.Count > 0)
            {
                for (var i = 0; i < profilesList.Count;i++ )
                {

                    r.UserName = profilesList[i].ToString();
                    r.UserRole = Roles.GetRolesForUser(profilesList[i]);
                    
                }
                
            }
            return View(r);
        }


My RolesAndUser class is like this


C#
public class RolesAndUser
    {
        public string UserName { get; set; }
        public string[] UserRole { get; set; }
    }


I want to use the object which i have passed from the Controller in a view

HTML
@model MvcApplication1.Models.RolesAndUser

@{
    ViewBag.Title = "Role Listing";
}

        
    
    <table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.UserName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UserRole)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.UserName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UserRole)
        </td>
        <td>
           @* @Html.ActionLink("Edit", "Edit", new { id=item.UserId }) |
            @Html.ActionLink("Details", "Details", new { id=item.UserId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.UserId })*@
            @Html.ActionLink("Create New Role", "RoleCreate") |
           @Html.ActionLink("Add Role to User", "RoleAddToUser") | 
           @Html.ActionLink("Check Roles for users", "GetRoles") | 
           @Html.ActionLink("Delete Role for user","DeleteRoleForUser")
        </td>
    </tr>
}

</table>


But it is throwing error at Foreach loop please tell me how i can use my populated object in a view
Posted
Comments
John C Rayan 13-Aug-15 4:16am    
You need List of RolesAndUser as you model.
In the controller , create a List of RolesAndUser and pass it to the view.
In the view , you define your model to the List of RolesAndUser.

Then you will be fine.

When you get an error always say what the error is, believe it or not but it makes a difference.

You are only returning a single object (RolesAndUser) so your Model is a single object, and you can't "foreach" on a single object. Your controller code isn't doing what you think it is doing either, it is just updating the same property, overwriting the previous version with the last version, you're not maintaining a collection of the results.

C#
public ActionResult RoleIndex()
        {
            var profilesList = db.UserProfiles.Select(u => u.UserName).ToList();
            List<rolesanduser> roles = new List<rolesanduser>() ;

            if (profilesList.Count > 0)
            {
                for (var i = 0; i < profilesList.Count;i++ )
                {
                    RolesAndUser r = new RolesAndUser();
                    r.UserName = profilesList[i].ToString();
                    r.UserRole = Roles.GetRolesForUser(profilesList[i]);
                    roles.Add(r);

                }

            }
            return View(roles);
        }


Your view now needs

@model List<MvcApplication1.Models.RolesAndUser>


and you can now "foreach" the Model in your view.
 
Share this answer
 
v2
Comments
lokopi.nagar 13-Aug-15 4:20am    
Thank you
Hi,

you are passing simply model to view that is incorrect
"@model MvcApplication1.Models.RolesAndUser"

you should pass model with (for bind list)
@model IEnumerable<mvcapplication1.models.rolesanduser>


for more information visit :

https://www.safaribooksonline.com/library/view/programming-razor/9781449312824/ch04.html[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900