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 に答える