我有一个 mvc4 应用程序。用户可以在其中创建新项目并为每个项目添加评论。(我无法解决评论添加部分)
我有两个模型 1.Comment
public partial class Comment
{
public int CommentID { get; set; }
public string Title { get; set; }
public int ProjectID { get; set; }
public string Rating { get; set; }
public virtual Project Project { get; set; }
}
2.项目
public partial class Project
{
public Project()
{
this.Comments = new HashSet<Comment>();
}
public int ProjectID { get; set; }
public string Name { get; set; }
public string Goal { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
现在我想在项目控制器索引页面上显示以下操作链接:编辑 | 详情 | 删除 | 添加评论(编辑、详细信息和删除功能工作正常,但无法创建评论)
这是我的项目控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectCreation.Models;
namespace ProjectCreation.Controllers
{
public class ProjectController : Controller
{
private ProjectCreationEntities db = new ProjectCreationEntities();
//
// GET: /Project/
public ActionResult Index()
{
return View(db.Projects.ToList());
}
//
// GET: /Project/Details/5
public ActionResult Details(int id = 0)
{
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
//
// GET: /Project/Create
public ActionResult Create()
{
return View();
}
//
// POST: /Project/Create
[HttpPost]
public ActionResult Create(Project project)
{
if (ModelState.IsValid)
{
db.Projects.Add(project);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
//
// GET: /Project/Edit/5
public ActionResult Edit(int id = 0)
{
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
//
// POST: /Project/Edit/5
[HttpPost]
public ActionResult Edit(Project project)
{
if (ModelState.IsValid)
{
db.Entry(project).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
//
// GET: /Project/Delete/5
public ActionResult Delete(int id = 0)
{
Project project = db.Projects.Find(id);
if (project == null)
{
return HttpNotFound();
}
return View(project);
}
//
// POST: /Project/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Project project = db.Projects.Find(id);
db.Projects.Remove(project);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
** public ActionResult Comment(int id = 0)
{
Comment comment = db.Comments.Find(id);
if (comment == null)
{
return HttpNotFound();
}
return View(comment);
}
[HttpPost]
public ActionResult Comment(Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(comment);
} **
}
}
我以某种方式无法在单击“添加评论链接”时创建评论。项目控制器的其余部分工作正常。这是我的评论控制器操作的视图页面:
@model ProjectCreation.Models.Comment
@{
ViewBag.Title = "Comment";
}
<h2>Comment</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Comment</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ProjectID)
</div>
<div class="editor-label">
@Html.EditorFor(model => model.ProjectID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rating)
@Html.ValidationMessageFor(model => model.Rating)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
有人可以指导我吗?我准确地传递了项目 ID。我似乎无法创建新评论。查看页面已打开,但没有任何更新。它甚至显示项目 id 而不是标签的下拉菜单。