私は mvc4 アプリケーションを持っています。ユーザーは新しいプロジェクトを作成し、各プロジェクトにコメントを追加できます。(コメント追加部分がわかりません)
私は 2 つのモデルを持っています。
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 のドロップダウンも表示されます。