我想創建一個使用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; }
}
}
你爲什麼要隱藏它?這似乎是IMO非常重要的信息。 –
Html.HiddenFor()? –