我有以下控制器,对于 _GetForSession
和 CommentForm
控制器,它们可以正常工作。但是当点击 _Submit
参数时,comment
对象为空。我的控制器类如下:
public class CommentController : Controller
{
//
// GET: /Comment/
public ActionResult Index()
{
return View();
}
public PartialViewResult _GetForSession(string isbnNo )
{
ViewBag.ISBN_No = isbnNo;
List<CommentModel> comments = CommentFacade.GetAllCommentsOnIsbn(isbnNo);
return PartialView("_GetForSession", comments);
}
[ChildActionOnly]
public PartialViewResult _CommentForm(string isbnNo)
{
CommentModel comment = new CommentModel() { ISBN_No = isbnNo };
return PartialView("_CommentForm", comment);
}
[ValidateAntiForgeryToken]
public PartialViewResult _Submit(CommentModel comment)
{
CommentFacade.SaveComment(comment);
List<CommentModel> comments = CommentFacade.GetAllCommentsOnIsbn(comment.ISBN_No);
ViewBag.ISBN_No = comment.ISBN_No;
return PartialView("_GetForSession", comments);
}
}
我认为如下:
视角- _GetForSession
@model IEnumerable<LibraryManagementWeb.Models.CommentModel>
<div id="comments">
<ul>
@foreach (var comment in Model)
{
<li>@comment.Comment</li>
}
</ul>
@using (Ajax.BeginForm("_Submit", "Comment", new AjaxOptions() { UpdateTargetId="comments"}))
{
@Html.AntiForgeryToken()
@Html.Action("_CommentForm", new { isbnNo= ViewBag.ISBN_No })
}
</div>
view - _CommentForm
@model LibraryManagementWeb.Models.CommentModel
<h2>_CommentForm</h2>
@Html.HiddenFor(model => model.ISBN_No)
<div>
@Html.EditorFor(model => model.ISBN_No)
<br />
@Html.LabelFor(model => model.Comment)
@Html.EditorFor(model => model.Comment)
</div>
<button type="submit">Submit Comment</button>
我尝试了所有可能的方法,但无法找到解决方法。我错过了什么?
编辑: Fiddler输出:
Fiddler原始视图如下:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/Account/Login?ReturnUrl=%2fBook%2fDetails%2f7">here</a>.</h2>
</body></html>