-1

我有一个 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 而不是标签的下拉菜单。

4

4 回答 4

1

您的控制器发送模型的另一个类型,然后是视图“想要”。类型必须相同

改变

@model IEnumerable<ProjectCreation.ViewModels.ProjectCreation>

@model List<ProjectCreation.ViewModels.ProjectCreation>
于 2013-06-10T14:04:57.150 回答
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);
}
于 2014-04-19T03:32:29.247 回答
0

您的视图说明了类型的模型,ProjectCreation 并且在您ActionResult的传递Project模型中。

你必须List<ProjectCreation>在你的 ActionResult 中返回

更新:

@Html.LabelFor(model => model.ProjectID)如果您希望您的 labelfor 助手显示您想要的名称,请在您的 viewModel 中使用 [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);
        } 
于 2013-06-10T14:24:13.270 回答
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");
    }
于 2013-06-11T14:23:41.413 回答