[英]Partial view- Controller receive null object
我有以下控制器,它适用于 _ 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>
视图 - _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 原始视图如下:
<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>
我认为问题在于您在局部视图中传递了不同的模型。 您需要创建一个 ViewModel,然后将相同的 ViewModel 传递给您的视图和不同的局部视图。 下面是一个例子,希望它能给你一个好主意。
视图模型
public class CommentViewModel
{
public List<CommentModel> CommentModels { get; set; }
public CommentModel CommentModel { get; set; }
}
控制器
public class CommentController : Controller
{
public ActionResult Index()
{
var model = new CommentViewModel()
{
CommentModels = listComments
};
return View(model);
}
public PartialViewResult _GetForSession(string isbnNo)
{
ViewBag.ISBN_No = isbnNo;
var model = new CommentViewModel
{
CommentModels = CommentFacade.GetAllCommentsOnIsbn(isbnNo);
};
return PartialView("_GetForSession", model);
}
[ChildActionOnly]
public PartialViewResult _CommentForm(string isbnNo)
{
var model = new CommentViewModel()
{
CommentModel = new CommentModel() {ISBN_No = isbnNo}
};
return PartialView("_CommentForm", model);
}
[ValidateAntiForgeryToken]
public PartialViewResult _Submit(CommentViewModel model)
{
CommentFacade.SaveComment(comment);
List<CommentModel> comments = CommentFacade.GetAllCommentsOnIsbn(comment.ISBN_No);
ViewBag.ISBN_No = comment.ISBN_No;
return PartialView("_GetForSession", model);
}
}
_GetForSession
@model Demo.Models.CommentViewModel
<div id="comments">
@using (Ajax.BeginForm("_Submit", "Home", new AjaxOptions() { UpdateTargetId = "comments" }))
{
@Html.AntiForgeryToken()
@Html.Action("_CommentForm", new { isbnNo = ViewBag.ISBN_No })
}
</div>
_评论表
@model Demo.Models.CommentViewModel
<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>
<input type="submit" value="Submit Comment" />
您的_Submit
Controller 方法需要标记为[HttpPost]
。 否则,它不会从正在提交的表单中读取数据。
在传递之前不要在ajax中对数据进行字符串化,只需将其以json格式传递而不进行字符串化。
function saveAttachments(surveyAttachments, surveyId) {
var data = new FormData();
var _files = $(surveyAttachments).prop("files");
console.log(_files);
for (i = 0; i < _files.length; i++) {
data.append(surveyId, _files[i]);
}
console.log(data);
$.ajax({
type: "POST",
url: _URLSaveAttachments,
dataType: "json",
data: data,
contentType: false,
processData: false,
success: function (response) {
},
failure: function (response) {
//alert(response.responseText);
},
error: function (response) {
//alert(response.responseText);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.