-2
[NonAction]
protected void PrepareNewsCommentModel(NewsCommentModel model, NewsComment newsComment)
{
  if (newsComment == null) throw new ArgumentNullException("newsComment");
  if (model       == null) throw new ArgumentNullException("model");

  model.Id = newsComment.Id;
  model.CommentTitle = newsComment.CommentTitle;
  model.CustomerName = newsComment.CustomerName;
  model.CommentText = newsComment.CommentText;

}

public ActionResult Comments(int number)
{
  if (!_newsSettings.Enabled)
    return RedirectToRoute("HomePage");

  var newsComment = _newsService.GetNewsComment(number);
  var model = new NewsCommentModel();
  PrepareNewsCommentModel(model,newsComment);

  return View(model);
}

这是我的错误:

Error 2 Argument 2: cannot convert from
'System.Collections.Generic.IList<Yapi.Core.Domain.News.NewsComment>' to
'Yapi.Core.Domain.News.NewsComment'
D:\YAPI\Projects\ASCS-Portal\Yapi.Web\Controllers\NewsController.cs 420
40
Yapi.Web
4

1 回答 1

2

错误告诉你问题。newsComment是 aIList<NewsComment>但您的PrepareNewsCommentModel方法需要 aNewsComment代替。

尝试使用 Linq 的First扩展方法:

var newsComment = _newsService.GetNewsComment(number).First();

或者FirstOrDefault,如果您的GetNewsComment方法可能返回一个空列表:

var newsComment = _newsService.GetNewsComment(number).FirstOrDefault();
于 2013-09-25T02:28:41.217 回答