0

我有我的控制器,我不确定我是否正确

[HttpGet]
[NoCache]
public ActionResult ListCommentsOnNews(int newsId, string newsComment) ???
{
  //code here with return
}

在我的 Article.aspx 视图中:

<div class="news-comment-content" id="news-comment-content">     
  <% if (Model.Results != null) 
    { %>
   <% foreach (var newsItem in Model.Results.NewsComments)
      {  %>  
      <% Html.RenderPartial("SetCommentOnNews", newsItem); %>
  <%} } %>
</div>

然后是我的部分 ListCommentsOnNews.ascx:

 <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<???>" %>

  <div class="news-post-list-item">
    <div class="news-post-user-info-wrapper">
      <div class="avatar">
        <img width="52" height="52" alt="Avatar" src="/ThemeFiles/Base/images/User/user-avatar.png" />
      </div>
      <div class="who-and-when-box">
        <%: newsItem.CommentDate %>  //get error here
        <br />
        <br />
        <%: ViewBag.UserName %>
      </div>
      <div class="news-comment"><%: newsItem.NewsComment %></div> //and get error here
      <div class="clear"></div>
    </div>     
    <div class="clear"></div> 
  </div>   

我的控制器:

   [HttpGet]
public ActionResult Article(int id, int? page)
{
  var news = ZincService.NewsService.GetNewsForId(id);
  var allNewsComments = ZincService.NewsService.GetAllNewsCommentsForId(id, page.GetValueOrDefault(1), 10);
  var currentUser = ZincService.GetUserForId(CurrentUser.UserId);

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

  if (allNewsComments != null)
  {
    var user = ZincService.GetUserForId(currentUser.UserId);
    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);
  }
  else
  {
    return View(SetNewsArticleViewModel(news,null));
  }
}

 [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;
}

我在 newsItem 部分收到错误,指出它在当前上下文中不存在。有人可以帮我吗?

4

1 回答 1

2

正如我从您发布的代码中推断的那样,问题在于您对模型的使用。您正在尝试使用模型属性,但您没有正确执行。请尝试以下操作:

<%
      Model.CommentDate
     %>

代替

<%: newsItem.CommentDate %>  //get error here
于 2012-12-21T10:57:13.857 回答