2012-12-04 56 views
0

我想創建一個使用MVC 4的博客,現在我有問題發送postid到評論創建頁面,並保持隱藏,直到我點擊創建以創建評論與帖子ID傳遞變量,並保持隱藏

這是註釋創建視圖

@model MVCProjectApp.Models.Comment 

@{ 
    ViewBag.Title = "Create Comment"; 
} 

<h2>Create</h2> 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Comments</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Username) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Username) 
      @Html.ValidationMessageFor(model => model.Username) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Message) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Message) 
      @Html.ValidationMessageFor(model => model.Message) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.Timestamp) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.Timestamp) 
      @Html.ValidationMessageFor(model => model.Timestamp) 
     </div> 


     <p> 
      <input type="submit" value="Create" /> 
     </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "~/FullPost/Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

這是我經過對帖子ID的註釋創建ActionLink的

@Html.ActionLink("Comment", "Create", "Comment", ID, null) 

這是評論控制器

// 
     // GET: /Comment/Create 

     public ActionResult Create() 
     { 
      ViewBag.PostID = new SelectList(db.Posts, "PostID", "Title"); 
      return View(); 
     } 

     // 
     // POST: /Comment/Create 

     [HttpPost] 
     public ActionResult Create(Comment comment) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Comments.Add(comment); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      ViewBag.PostID = new SelectList(db.Posts, "PostID", "Title", comment.PostID); 
      return View(comment); 
     } 

這是註釋模型(如上面的評論中提到)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MVCProjectApp.Models 
{ 
    public class Post 
    { 
     public int PostID { get; set; } 
     public string Title { get; set; } 
     public string Message { get; set; } 
     public DateTime Timestamp { get; set; } 
     public virtual ICollection<Comment> Comments { get; set; } 
    } 
    public class Comment 
    { 
     public int CommentID { get; set; } 
     public int PostID { get; set; } 
     public string Username { get; set; } 
     public string Message { get; set; } 
     public DateTime Timestamp { get; set; } 
     public virtual Post Post { get; set; } 
    } 
} 
+1

你爲什麼要隱藏它?這似乎是IMO非常重要的信息。 –

+1

Html.HiddenFor()? –

回答

0

使用Html.HiddenFor()來保存該值您需要從視圖中返回給您,但不想顯示。

請記住,這是可以破解的。採取適當措施確保您傳遞給視圖的ID與返回的ID相同。查看AntiForgeryToken。

+0

我碰到一個錯誤,說{「INSERT語句與FOREIGN KEY約束衝突\」FK_dbo.Comments_dbo.Posts_PostID \「。Confliect發生在數據庫\」MVCProjectApp.Models.DBPostsContext \「,table \」dbo.Posts \「,'PostID'列。\ r \ n聲明已經終止。」}我迷路了,不知道該怎麼辦 – Scott