【问题标题】:Setting values for Get and Post设置 Get 和 Post 的值
【发布时间】:2013-12-19 22:17:09
【问题描述】:

我正在开发一个 MVC C# 应用程序。

这是我的代码:

     //
    // GET: /Comment/Create
    [Authorize]
    public ActionResult Create(int bookid = 0)
    {
        var comment = new Comment();
        comment.Book = _bookService.GetBookFromBookId(bookid);
        comment.Owner = _userService.GetLoggedInUser();
        return View(comment);
    }

    //
    // POST: /Comment/Create

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

        return View(comment);
    }

在 Get Create ActionResult 中,为 Comment 对象设置 Book 和 Owner 值,但是在 HttpPost Create ActionResult 中,值不在对象中。

我怎样才能让这段代码正常工作?

更新

这是我的查看代码:

@model LearningTestMVCApplication.Models.Comment

@{
ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Comment</legend>
    <p>
        Book Name: @Html.DisplayFor(model =>  model.Book.Name)
    </p>
    <div class="editor-label">
        @Html.LabelFor(model => model.DisplayText)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.DisplayText)
        @Html.ValidationMessageFor(model => model.DisplayText)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>
</fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

我已经调试了View,对象属性都在那里...

【问题讨论】:

  • 您的视图是什么样的?您是否尝试过调试以查看评论对象是否具有预期值?
  • 你能发布你的视图的内容吗?在没有看到您的视图的情况下,我会首先查看您输入的“名称”属性,并确保它们与您的评论模型的属性名称匹配。

标签: c# asp.net-mvc view model http-post


【解决方案1】:

在您的 POST 操作中,对象中唯一存在的部分是实际在表单中由字段表示的数据。如果您没有为图书和用户提供字段(隐藏或其他),那么它们将不会出现在您的帖子中,并且您的模型将不会有这些对象。

但是,在这种情况下,您无论如何都不想这样做。您不允许他们更改该信息;您只是在创建关联。所以你应该拥有的是:

// GET: /Comment/Create
[Authorize]
public ActionResult Create(int bookid)
{
    var comment = new Comment();
    return View(comment);
}

//
// POST: /Comment/Create

[HttpPost]
[Authorize]
[ValidateAntiForgeryToken]
public ActionResult Create(int bookid, Comment comment)
{
    comment.Book = _bookService.GetBookFromBookId(bookid);
    comment.Owner = _userService.GetLoggedInUser();

    if (ModelState.IsValid)
    {
        db.Comments.Add(comment);
        db.SaveChanges();
        return RedirectToAction("Index","Book",null);
    }

    return View(comment);
}

看到区别了吗?您的 POST 操作还应该采用图书 ID,它将使用它来查找正确的图书以保存其余的对象数据。 GET 操作现在只使用图书 ID 来标识要发布到的 URL。 (您现在只需执行 @Html.BeginForm() 即可,无需传递任何路由参数即可将其回发到相同的 URL。)您可能仍想在 GET 操作中查找该书,但是这仅用于在视图中显示;它实际上不会影响发送回 POST 操作的数据。

用户信息应始终由 POST 操作控制。您永远不应该发布类似事物所有者之类的内容,因为任何帖子值都可以被操纵,从而允许恶意用户将对象与他们喜欢的任何内容相关联。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    • 2015-07-21
    • 2015-04-21
    • 2016-05-04
    相关资源
    最近更新 更多