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.
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.