【问题标题】:ASP.NET MVC: id not sent back on post or delete to the 'view by id'ASP.NET MVC:id 未在发布或删除时发回“按 id 查看”
【发布时间】:2015-09-05 21:06:42
【问题描述】:

我正在尝试将 cmets 添加到从创建的主题列表中选择的主题中, 我有一个主题列表,我选择其中一个.. 由于主题的 id,所选主题会使用其 cmets 加载其视图。 在选定的主题视图上,我想添加评论,因此在发表评论时,它会添加评论(在表格中)但无法返回或重定向到视图(带有 cmets 的主题),因为该视图是结果该主题的 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'.

下面是我在主题中加载 cmets 的代码....

“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);
    }

下面是添加cmets的代码

我尝试通过会话将主题 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);
    }

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • @Boman。你能解释一下吗,因为我对隐藏字段不是很熟悉
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2012-02-28
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多