ASP.NET MVC:id不会在帖子上发回或通过id'删除到'视图

时间:2015-06-19 19:11:10

标签: asp.net-mvc http-post http-get

我正在尝试为从创建的主题列表中选择的主题添加注释, 我有一个主题列表,我选择其中一个.. 由于主题的ID,所选主题会使用其注释加载其视图。 在选定的主题视图中,我想添加注释,因此在注释的帖子上,它添加注释(在表中)但不能返回或重定向到查看(带注释的主题)因为该视图是该主题ID的结果。 所以我正在寻找一种方法,在添加带有主题视图ID的注释后返回视图... 还是有另一种解决方法,.. 我真的需要帮助......

这是我得到的错误

Server Error in '/' Application.

The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'TechForum.Models.ForumViewModel'.

下面是我在代码中加载评论的代码....

' int id'是主题列表中主题的ID

[HttpGet]
    public ActionResult Comments(ForumViewModel model, int id)
    {
        DataSet ds = WebService.GetCommentsByTopic(id);

        List<CommentModel> commentModelList = new List<CommentModel>();

        DataTable dt = ds.Tables[0];

        foreach (DataRow dr in dt.Rows)
        {
            CommentModel _commentModel = new CommentModel();

            _commentModel.COMMENTID = Int32.Parse(dr["COMMENTID"].ToString());
            _commentModel.TOPICID = Int32.Parse(dr["TOPICID"].ToString());
            Session["topicid"] = Int32.Parse(dr["TOPICID"].ToString());
            _commentModel.COMMENT = dr["COMMENT"].ToString();
            _commentModel.COMMENT_DATE = DateTime.Parse(dr["COMMENT_DATE"].ToString());

            commentModelList.Add(_commentModel);
        }
        model.COMMENTLIST = commentModelList;

        return View(model);
    }

下面是添加评论的代码

我尝试通过会话

将主题ID从httpget评论视图传递到帖子评论
[HttpPost]
    public ActionResult Comments(ForumViewModel model)
    {
        try
        {

            WebService.AddComment(int.Parse(Session["memberid"].ToString()), int.Parse(Session["topicid"].ToString()), model.COMMENTS.COMMENT, DateTime.Parse(model.COMMENTS.COMMENT_DATE.ToString()));

             int id = int.Parse(Session["topicid"].ToString());
               ViewData["output"] = "Comment Added";
               return View("Comments", id);

        }
        catch (Exception er)
        {
            ViewBag.errormsg = er.Message;
            ViewData["output"] = "Comment not added";
        }
        return RedirectToAction("Comment", int.Parse(Session["topicid"].ToString()));
    }

同样类似的删除问题,我尝试将两个id传递给视图,如下所示,一个用于删除注释的id以及另一个(id2)发回的视图主题的ID

@Html.ActionLink("x", "DeleteComment", new { id = item.COMMENTID, id2 = item.TOPICID}
下面的

是删除操作

public ActionResult DeleteComment(int id, int id2)
    {
        WebService.DeleteComment(id);
        ForumViewModel model = new ForumViewModel();

        DataSet ds = WebService.GetCommentsByTopic(id2);

        List<CommentModel> commentModelList = new List<CommentModel>();

        DataTable dt = ds.Tables[0];

        foreach (DataRow dr in dt.Rows)
        {
            CommentModel _commentModel = new CommentModel();
            _commentModel.COMMENTID = Int32.Parse(dr["COMMENTID"].ToString());
            _commentModel.TOPICID = Int32.Parse(dr["TOPICID"].ToString());
            _commentModel.MEMBERID = Int32.Parse(dr["MEMBERID"].ToString());
            _commentModel.COMMENT = dr["COMMENT"].ToString();
            _commentModel.COMMENT_DATE = DateTime.Parse(dr["COMMENT_DATE"].ToString());

            commentModelList.Add(_commentModel);

        }
        model.COMMENTLIST = commentModelList;
        ViewData["success"] = "Comment Deleted";
        return RedirectToAction("Comments", model);
    }

1 个答案:

答案 0 :(得分:0)

您可以将TopicId放在ForumViewModel中,并将其作为视图中的隐藏字段设置为您想要的,以便在模型中传递。不明白为什么那不起作用。