ASP.NET MVC: id not sent back on post or delete to the 'view by id'

130 views Asked by At

I am trying to add comments to a topic selected from a list of topics created, I have a list of topics and I select one of them.. the selected topic loads its view with its comments due to the id of the topic. On the selected topic view, I want to add a comment, so on post of the comment, it adds the comment(in the table) but can't return or redirect to view (topics with comments) because that view is as a result of that topic id. so am looking for a way whereby its returns back to the view after adding a comment with the id of the topic view... or is there another way of solving this,,.. I really need assistance here...

this is the error i get

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'.

below is my code that loads the comments in a topic....

The 'int id' is the id of the topic from a list of topics

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

below is the code for adding comments

I tried passing the topic id from the httpget comment view to the post comment through a session

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

the same similar issue for the delete of which I tried passing two id's to the view as shown below, one to take the id of the comment its about to delete and the other (id2) to send back the id of the topic to the view

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

below is the delete action

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

There are 1 answers

1
Zarthost Boman On

Can you just put the TopicId in the ForumViewModel and have it as a hidden field in the view that is set to what you want so it is passed around in the model. Can't see why that wouldn't work.