0

我正在尝试从多个模型中获取数据到我的视图中,但是每次遇到错误时,我都会尝试很多方法来解决此问题,但失败了...请帮助我解决此问题..我正在发布我的代码..n我为此创建了视图模型

这是一个索引视图

@model Stubify.Models.ViewModels.TimeLine

   <div class="media social-box social-composer ">
                <a class="pull-left social-users-avatars" href="#">
                    <img class="media-object" width="55" height="55" src="../Content/assets/img/avatar-55.png" alt="Julio Marquez">
                </a>
                <div class="media-body social-body">
                    @foreach (var item in Model.User.Take(1))
                    {
                        <textarea id="new_message" name="new_message" class="textarea span12" placeholder="What's happening, @item.FullName?" cols="10"></textarea>
                    }

                    <div class="social-footer">
                        <div class="btn-group">
                            <a class="btn" data-original-title=""><i class="icon-map-marker"></i></a>
                            <a class="btn" data-original-title=""><i class="icon-camera"></i></a>
                        </div>
                        <button class="btn btn-primary pull-right" type="button">Post</button>
                    </div>
                </div>
            </div>


            <div id="NewsFeeds">
                @Html.Partial("_NewsFeeds", Model)
            </div>
            <div id="loading"></div>

这是一个视图模型

public class TimeLine
{
    public virtual IEnumerable<Users> User { get; set; }
    public virtual IEnumerable<UsersPosts> UsersPost { get; set; }
}

这是部分视图

  @model Stubify.Models.ViewModels.TimeLine
@foreach(var item in Model)
{
<div class="social-share-image">
            <img src="../Content/assets/img/gallery/sunset.jpg" alt="Sun Set">
            <div class="social-share-image-text">
                <h4>@item.UserId</h4>
                <p><strong>@item.PostContent!</strong><small> @item.Date <i class="icon-globe"></i></small></p>
            </div>
        </div>
}

这是一个控制器

public ActionResult Index(string searchTerm = null, int? id = 0)
    {
        var viewModel = new TimeLine();

        var page = id ?? 0;

        viewModel.User = db.Users.OrderByDescending(u => u.Id)
            .Where(u => searchTerm == null || u.FullName.ToLower().Contains(searchTerm)).Take(records);
        if (Request.IsAjaxRequest())
        {
            return PartialView("_NewsFeeds", GetPaginatedProducts(page));
        }
        return View(viewModel);
    }

请帮帮我

4

1 回答 1

0

最明显的问题是,当您填写viewModel.User = ...Controller 时,没有代码填写viewModel.UsersPost = ...,因此将是null.

我注意到这部分永远无法工作:

@model Stubify.Models.ViewModels.TimeLine
@foreach(var item in Model)
{
   ...
}

TimeLine不是一个集合(尽管它包含 2 个集合)。

这可能是您的意思:

@model IEnumerable<Stubify.Models.Models.User>  // my best guess
@foreach(var item in Model)
{
   ...
}
于 2013-06-23T18:35:54.080 回答