我有一个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而不是标签的下拉。
答案 0 :(得分:1)
您的控制器发送anoter模型类型,然后发送视图'想要'。类型必须相同
更改
@model IEnumerable<ProjectCreation.ViewModels.ProjectCreation>
到
@model List<ProjectCreation.ViewModels.ProjectCreation>
答案 1 :(得分:1)
问题在于:
[HttpPost]
public ActionResult Comment(Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(comment);
}
您忘记加载数据库并添加Create函数()。它应该是这样的:
[HttpPost]
public ActionResult Comment(Comment comment)
{
if (ModelState.IsValid)
{
using (var db = new ProjectCreationEntities())
{
var user = db.Comments.Create();
user.CommentId = comment.CommentID;
user.Title = comment.Title;
user.ProjectId = comment.ProjectId;
user.Rating = comment.Rating;
db.Comments.Add(user);
db.SaveChanges();
}
return RedirectToAction("Index");
}
return View(comment);
}
答案 2 :(得分:0)
您的观点会显示ProjectCreation
类型的模型
并且在ActionResult
你正在通过Project
模型。
您必须在ActionResult中返回List<ProjectCreation>
@Html.LabelFor(model => model.ProjectID)
使用[DisplayName(“PropertyName”)]属性
public class Comment
{
[DisplayName("PropertyName")]
public int ProjectId{get;set;}
}
要创建您的评论,您需要使用HttpGet请求的ActionResult
[HttpGet]
public ActionResult Comment()
{
var model = new Comment();
return View(model);
}
只有这样你才能发布它 你的ActionResult for Post:
[HttpPost]
public ActionResult Comment(Comment comment)
{
if (ModelState.IsValid)
{
db.Comments.Add(comment);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(comment);
}
答案 3 :(得分:0)
也许我误解了你的代码,但在我看来,你只有代码来编辑和保存评论,而不是创建新的代码。
此代码段来自您自己的代码。在第一个函数中,您只允许显示注释(如果它们已存在)。但是你如何展示一个未创造的形式呢?在我看来,当你尝试创建一个新的评论(并且还没有id)时,你最终会得到HttpNotFound()的回复。
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);
}
我会尝试添加一个新动作来创建新评论,可能是这样的:
public ActionResult CreateComment()
{
return View("Comment");
}