在一对多关系中根据其他项目 ID 添加项目

add item base on other item id in one to many relation

我有这个 razor 视图,其中包含一些基于某些条件的项目,我需要向任何项目添加评论(就像 SO 中的评论一样)。 试图将 id 传递给不同的表单没有用, 这就是我所做的,但我想这是错误的方法。 到目前为止我有这个: 根据用户(参数)显示评论

     public async Task<ActionResult> ReviewAbout(int? id)
    {
        var profilec = await db.profile.FindAsync(id);

        IEnumerable<ReviewViewModel> viewModel = from review in db.reviews.OrderBy(d => d.Created)
                                                 .Include(s => s.subject).Where(m => m.subject.MemberID == profilec.MemberID)
                                                 .Include(r => r.Writer)
                                                 join comment in db.Comments
                                                 on review.ReviewId equals comment.ReviewId into joinedReviewComment
                                                 select new ReviewViewModel
                                                 {
                                                     //parameters
                                                     ReviewId = review.ReviewId,

                                                     Comments = joinedReviewComment.Select(c => new CommentViewModel
                                                     {
                                                         CommentBody = c.CommentBody,
                                                         CommentGBU = c.CommentGBU,
                                                         anonymous = c.anonymous,

                                                         CommentsWriterName = c.CommentsWriterName
                                                     }).ToList(),
                                                 };

        return View(viewModel.ToList());
    }

在视图中

    @model IList<MyWebSite.ViewModels.ReviewViewModel>
@for (int i = 0; i < Model.Count(); i++)
{
    @Html.DisplayFor(x => x[i].ReviewId)

    @Html.HiddenFor((x => x[i].ReviewId))

    @Html.ActionLink("comment", "AddComent", new { id = @Html.DisplayFor(x => x[i].ReviewId) })


    @for (var j = 0; j < Model[i].Comments.Count; j++)
    {
    }

}

我试图将 id 传递给视图并获取 post 控制器看起来像这样:

  public async Task <ActionResult > AddComent(Comments commnets )
    {


        //commnets.ReviewId = commnets.ReviewId;
        //commnets.ReviewId = commnets.review.ReviewId;
        //commnets.ReviewId = commnets.ReviewId;

        if (ModelState.IsValid)
        {
            db.Comments.Add(commnets);
            await db.SaveChangesAsync();
            return RedirectToAction("index");
        }
        return View(commnets);
    }

但是,运气不好,最好的做法是什么?

问题是 AddComent 方法等待评论模型和您的视图发布 ReviewViewModel。因此,有两种解决方法:

1) 如果只是传递id值使用下面的方法,然后在AddComent方法中使用相同的参数名称如下所示:

查看:

@Html.ActionLink("Comment", "AddComent", new { comment = item.ID }) 

控制器:

public async Task <ActionResult> AddComent(Comment comment)
{
    ...
}


2) 您可以将 ViewModel 传递给 Controller,然后尝试将 ViewModel 映射到 DomainModel,如下所示:

public async Task <ActionResult > AddComent(IEnumarable<ReviewViewModel> reviews)
{
    if (ModelState.IsValid)
    {

        //Mapping ViewModel to DomainModel (Review > ReviewViewModel)
        var viewModel = new ReviewViewModel();
        AutoMapper.Mapper.CreateMap<Review , ReviewViewModel>();
        AutoMapper.Mapper.Map(model, viewModel);
        //
        db.Comments.Add(model);
        await db.SaveChangesAsync();
        return RedirectToAction("index");
    }
    return View(model);
}

有关详细信息,请访问 http://automapper.org/。希望这有助于...