如何从控制器传递类的对象到视图? [英] How to pass an object of class from controller to view?

查看:87
本文介绍了如何从控制器传递类的对象到视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力为所有这样的用户获取角色。



I am trying to get the Roles for all users like this.

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);
        }





我的RolesAndUser类是这样的







My RolesAndUser class is like this


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





我想在视图中使用我从Controller传递的对象





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

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





但它在Foreach循环中抛出错误请告诉我如何在视图中使用我的填充对象



But it is throwing error at Foreach loop please tell me how i can use my populated object in a view

推荐答案

当你得到一个错误总是说错误是什么,信不信由你有所不同。



您只返回一个对象(RolesAndUser),因此您的模型是单一的对象,你不能在一个对象上预约。你的控制器代码没有做你认为它正在做的事情,它只是更新相同的属性,用最后一个版本覆盖以前的版本,你没有维护结果的集合。



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.






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>





欲了解更多信息,请访问:



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


这篇关于如何从控制器传递类的对象到视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆