如何在mvc中查看列表对象?

时间:2016-03-01 08:54:57

标签: model-view-controller

在控制器页面上:

object likes = (from p in ctx.Profiles
                join sl in ctx.SuggestionLikes on p.UserId equals sl.UserId
                join s in ctx.Suggestions on sl.SuggestionId equals s.Id
                join u in ctx.Users on s.Crtr equals u.Id
                where u.Email == mail
                orderby sl.LikeDate descending
                select new LikeClass { UserName = p.UserName, UserId = p.UserId, PhotoUrl = p.PhotoUrl, OneriId = s.Id, LikeDate = sl.LikeDate }).Take(10).ToList();


            object comments = (from p in ctx.Profiles
                            join sc in ctx.SuggestionComments on p.UserId equals sc.Crtr
                            join s in ctx.Suggestions on sc.SuggestionId equals s.Id
                            join u in ctx.Users on s.Crtr equals u.Id
                            where u.Email == mail
                            orderby sc.Crtm descending
                            select new CommentClass { UserName = p.UserName, UserId = p.UserId, PhotoUrl = p.PhotoUrl, OneriId = s.Id, Crtm = sc.Crtm }).Take(10).ToList();


            object follows = (from u in ctx.Users
                           join f in ctx.Follows on u.Id equals f.FollowerId
                           where f.FollowingId == user_id
                           orderby f.FollowDate descending
                           select new FollowClass { UserName = u.UserName, FollowDate = f.FollowDate, UserId = u.Id, ThumbnailUrl = u.ThumbnailUrl }).Take(10).ToList();
            List<object> myModel = new List<object>();
            myModel.Add(likes);
            myModel.Add(comments);
            myModel.Add(follows);
            return View(myModel);
在视图页面

@model IEnumerable<object>
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/MasterPage/_Layout.cshtml";
    List<LikeClass> list_like = Model.ToList()[0] as List<LikeClass>;
    List<CommentClass> list_comment = Model.ToList()[1] as List<CommentClass>;
    List<FollowClass> list_follow = Model.ToList()[2] as List<FollowClass>;
}


@foreach (var item in list_like)
{
    <tr>
        <td>@item.UserName</td>
        <td>@item.UserId</td>
        <td>@item.PhotoUrl</td>
        <td>@item.OneriId</td>
        <td>@item.LikeDate</td>
        <td></td>
    </tr>
}
<br />
@foreach (var item in list_comment)
{
    <tr>
        <td>@item.UserName</td>
        <td>@item.UserId</td>
        <td>@item.PhotoUrl</td>
        <td>@item.OneriId</td>
        <td>@item.Crtm</td>
    </tr>
}
<br />
@foreach (var item in list_follow)
{
    <tr>
        <td>@item.UserName</td>
        <td>@item.UserId</td>
        <td>@item.ThumbnailUrl</td>
        <td>@item.FollowDate</td>
    </tr>
}

我想在视图页面中显示对象项。但是它没有工作。返回空值。请帮忙。

Exapmle:item.UserName是null references.but在sql查询中它不是null值。

0 个答案:

没有答案