2015-06-19 72 views
0

我想添加評論,從主題列表中選擇一個主題創建, 我有一個主題列表,我選擇其中之一.. 由於主題的ID,所選主題將加載其視圖及其註釋。 在選定的主題視圖上,我想添加評論,所以在評論發佈後,它會添加評論(在表格中),但無法返回或重定向到查看(具有評論的主題),因爲該視圖是作爲該主題ID的結果。 所以我正在尋找一種方式,其中返回到視圖後,添加一個評論與主題視圖的ID ... 或有另一種解決這個問題的方法,, .. 我真的需要幫助在這裏...ASP.NET MVC:身份證未發送回發帖或刪除到'查看身份證'

這是錯誤我得到

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

是我的代碼是從列表加載一個話題....在評論

的「廉政身份證」爲主題的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); 
    } 
下面

是添加註釋

代碼

我試圖從HTTPGET評論查看到後評論傳遞主題ID通過會話

[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的該視圖,如下圖所示,一個帶註釋的它要刪除和其他(ID2)的話題的ID發送回視圖中的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); 
    } 

回答

0

你能不能把TopicId在ForumViewModel,並把它作爲一個隱藏字段中被設置爲你想要什麼,以便它在模型周圍傳遞的視圖。看不出爲什麼這是行不通的。

+0

@Boman。你能解釋一下嗎,因爲我不太熟悉隱藏領域 –