0

我的控制器中有这个:

public ActionResult Article(int id, int? page)
{
  var news = ZincService.NewsService.GetNewsForId(id);
  var allNewsComments = ZincService.NewsService.GetAllNewsCommentsForId(id, page.GetValueOrDefault(1), 10);

  if (news == null || news.PublishingState != PublishingState.Live)
    return RedirectToAction("NotFound");

  var user = ZincService.GetUserForId(id);
  if (user == null || user.Customer.CustomerId != CurrentCustomer.CustomerId)
    return DataNotFound();

  List<NewsCommentsViewModel> newsItemsViewModel = new List<NewsCommentsViewModel>();

  foreach (var newsItem in allNewsComments.NewsComments)
  {
    NewsCommentsViewModel newsItemViewModel = new NewsCommentsViewModel();
    newsItemViewModel.Results = allNewsComments;
    newsItemViewModel.CurrentPage = page.GetValueOrDefault(1);
    newsItemViewModel.PageSize = 10;
    newsItemsViewModel.Add(newsItemViewModel);
  }

  ViewBag.Avatar = user.UserImage;
  ViewBag.UserName = user.Firstname + "   " + user.Surname;
  NewsCommentsViewModel model = (NewsCommentsViewModel)SetNewsArticleViewModel(news, newsItemsViewModel); 
  model.Results.NewsComments = allNewsComments.NewsComments;
  return View(model);
}

  [NonAction]
private NewsArticleViewModel SetNewsArticleViewModel(Entities.News.News news, NewsArticleViewModel viewModel)
{
  viewModel.News = news;
  viewModel.IsFavourite = ZincService.FavouriteService.IsFavouriteForUser(CurrentUser.UserId, news); 
  viewModel.DownloadAttachments = news.NewsAttachments.Where(x =>
    Core.FileFormat.FileFormatHelper.GetFileFormatType(x.FileExtension) == Core.FileFormat.FileFormatType.Excel ||
    Core.FileFormat.FileFormatHelper.GetFileFormatType(x.FileExtension) == Core.FileFormat.FileFormatType.PDF ||
    Core.FileFormat.FileFormatHelper.GetFileFormatType(x.FileExtension) == Core.FileFormat.FileFormatType.PowerPoint ||
    Core.FileFormat.FileFormatHelper.GetFileFormatType(x.FileExtension) == Core.FileFormat.FileFormatType.Word);

  viewModel.EmbedAttachments = news.NewsAttachments.Where(x =>
    Core.FileFormat.FileFormatHelper.GetFileFormatType(x.FileExtension) == Core.FileFormat.FileFormatType.Video);

  return viewModel;
}

public class NewsCommentsViewModel : NewsArticleViewModel
{
  public int CurrentPage { get; set; }
  public int PageSize { get; set; }
  public Models.News.NewsCommentsDataModelResults Results { get; set; }
}

public class NewsArticleViewModel
{
  public Entities.News.News News { get; set; }
  public bool IsFavourite { get; set; }
  public IEnumerable<Entities.News.NewsAttachment> DownloadAttachments { get; set; }
  public IEnumerable<Entities.News.NewsAttachment> EmbedAttachments { get; set; }    
}

allNewsComments.NewsComments 包含我所有的新闻评论,但得到和错误然后说明:System.NullReferenceException:{“对象引用未设置为对象的实例。”}

谢谢

4

1 回答 1

0

我对其进行了排序:

public ActionResult Article(int id, int? page)
{
  var news = ZincService.NewsService.GetNewsForId(id);
  var allNewsComments = ZincService.NewsService.GetAllNewsCommentsForId(id, page.GetValueOrDefault(1), 10);

  if (news == null || news.PublishingState != PublishingState.Live)
    return RedirectToAction("NotFound");

  var user = ZincService.GetUserForId(id);
  if (user == null || user.Customer.CustomerId != CurrentCustomer.CustomerId)
    return DataNotFound();

  ViewBag.Avatar = user.UserImage;
  ViewBag.UserName = user.Firstname + "   " + user.Surname;
  NewsCommentsViewModel model =  (NewsCommentsViewModel)SetNewsArticleViewModel(news, new NewsCommentsViewModel());
  **foreach (var newsItem in allNewsComments.NewsComments)
  {
    model.Results = allNewsComments;
    model.PageSize = 10;
    model.CurrentPage = page.GetValueOrDefault(1);
  }**
  return View(model);
}
于 2012-12-11T12:22:28.827 回答