Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 mvc4中显示错误的视图模型_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc 4 mvc4中显示错误的视图模型

Asp.net mvc 4 mvc4中显示错误的视图模型,asp.net-mvc-4,Asp.net Mvc 4,我有一个mvc4应用程序,用户可以在其中创建新项目并对每个项目添加评论。我无法理解添加注释的部分 我有两种型号 1.评论 public partial class Comment { public int CommentID { get; set; } public string Title { get; set; } public int ProjectID { get; set; } public string Rati

我有一个mvc4应用程序,用户可以在其中创建新项目并对每个项目添加评论。我无法理解添加注释的部分

我有两种型号 1.评论

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; }
    }
我无法在点击添加评论链接时创建评论。项目控制员的其余部分工作正常。 这是我的评论控制器操作的查看页面:

@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的下拉列表,而不是标签。

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

改变

@model IEnumerable<ProjectCreation.ViewModels.ProjectCreation>


您的视图显示ProjectCreation类型的模型 在您的ActionResult中,您正在传递项目模型

您必须在ActionResult中返回列表

更新: @Html.LabelFormodel=>model.ProjectID如果希望labelfor助手显示所需的名称,请在viewModel中使用[DisplayNamePropertyName]属性

public class Comment
{
[DisplayName("PropertyName")]
public int ProjectId{get;set;}
}
要创建您的评论,您需要带有HttpGet请求的ActionResult

[HttpGet]
public ActionResult Comment()
{
     var model = new Comment();
     return View(model);
}
只有到那时你才能发布它 您发布的操作结果:

   [HttpPost]
    public ActionResult Comment(Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(comment);
        } 

也许我有点误解了你的代码,但在我看来,你只有编辑和保存注释的代码,而不是创建新注释的代码

此代码段来自您自己的代码。在第一个函数中,仅当注释已经存在时才允许显示注释。但是如何为未创建的表单显示表单呢?在我看来,当你试图创建一个新的评论,但还没有一个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");
    }
[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);
}
问题是:

[HttpPost]
public ActionResult Comment(Comment comment)
{
    if (ModelState.IsValid)
    {
        db.Comments.Add(comment);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(comment);
}
您忘记加载数据库并添加Create函数。应该是这样的:

    public ActionResult CreateComment()
    {
        return View("Comment");
    }
[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);
}

您不了解错误的哪一部分?这会导致编译错误“System.Collections.Generic.List”不包含“Project”的定义,并且找不到接受类型为“System.Collections.Generic.List”的第一个参数的扩展方法“Project”是否缺少using指令或程序集引用?