设置要传递给视图的模型的问题

时间:2012-12-11 10:52:43

标签: asp.net-mvc-3 oop

我在控制器中有这个:

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:{“对象引用未设置为对象的实例。”}

由于

1 个答案:

答案 0 :(得分: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);
}