C# 部分视图-控制器接收空对象
我有以下控制器,它可以正确地用于C# 部分视图-控制器接收空对象,c#,asp.net-mvc,C#,Asp.net Mvc,我有以下控制器,它可以正确地用于GetForSession和CommentForm控制器。但是当点击\u Submit参数comment时,对象为空。我的控制器类如下所示: public class CommentController : Controller { // // GET: /Comment/ public ActionResult Index() { return View();
GetForSession
和CommentForm
控制器。但是当点击\u 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);
}
}
<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>
公共类控制器:控制器
{
//
//获取:/Comment/
公共行动结果索引()
{
返回视图();
}
public PartialViewResult_GetForSession(字符串isbnNo)
{
ViewBag.ISBN_No=isbnNo;
List comments=CommentFacade.GetAllCommentsOnIsbn(isbnNo);
返回PartialView(“_GetForSession”,comments);
}
[仅限儿童]
public PartialViewResult\u CommentForm(字符串isbnNo)
{
CommentModel comment=new CommentModel(){ISBN_No=isbnNo};
返回PartialView(“_CommentForm”,comment);
}
[ValidateAntiForgeryToken]
公共部分评论结果提交(评论模型评论)
{
CommentFacade.SaveComment(comment);
List comments=CommentFacade.GetAllCommentsOnIsbn(comment.ISBN_No);
ViewBag.ISBN_No=comment.ISBN_No;
返回PartialView(“_GetForSession”,comments);
}
}
我的意见如下:
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);
}
}
<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>
查看-\u 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>
@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 IEnumerable
@foreach(模型中的var注释)
{
- @评论,评论
}
@使用(Ajax.BeginForm(“_Submit”,“Comment”,new AjaxOptions(){UpdateTargetId=“comments”}))
{
@Html.AntiForgeryToken()
@Action(“_CommentForm”,new{isbnNo=ViewBag.ISBN_No})
}
查看-\u评论表单
@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>
@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" />
@model LibraryManagementWeb.Models.CommentModel
_评论形式
@Html.HiddenFor(model=>model.ISBN\u No)
@EditorFor(model=>model.ISBN\u No)
@LabelFor(model=>model.Comment)
@EditorFor(model=>model.Comment)
提交评论
我尝试了所有可能的方法,但找不到解决办法。我错过了什么
编辑:
小提琴手发出:
fiddler原始视图如下所示:
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);
}
}
<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; }
}
公共类CommentViewModel
{
公共列表模型{get;set;}
公共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);
}
}
公共类控制器:控制器
{
公共行动结果索引()
{
var模型=新的CommentViewModel()
{
CommentModels=listComments
};
返回视图(模型);
}
public PartialViewResult_GetForSession(字符串isbnNo)
{
ViewBag.ISBN_No=isbnNo;
var模型=新模型
{
CommentModels=CommentFacade.GetAllCommentsOnIsbn(isbnNo);
};
返回PartialView(“GetForSession”,模型);
}
[仅限儿童]
public PartialViewResult\u CommentForm(字符串isbnNo)
{
var模型=新的CommentViewModel()
{
CommentModel=新CommentModel(){ISBN\u No=isbnNo}
};
返回PartialView(“\u CommentForm”,model);
}
[ValidateAntiForgeryToken]
公共部分视图结果提交(CommentViewModel模型)
{
CommentFacade.SaveComment(comment);
List comments=CommentFacade.GetAllCommentsOnIsbn(comment.ISBN_No);
ViewBag.ISBN_No=comment.ISBN_No;
返回PartialView(“GetForSession”,模型);
}
}
\u 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>
@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
@使用(Ajax.BeginForm(“_Submit”,“Home”,new AjaxOptions(){UpdateTargetId=“comments”}))
{
@Html.AntiForgeryToken()
@Action(“_CommentForm”,new{isbnNo=ViewBag.ISBN_No})
}
\u评论形式
@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>
@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" />
@model Demo.Models.CommentViewModel
_评论形式
@*@Html.HiddenFor(model=>model.ISBN\u No)*@
@EditorFor(model=>model.ISBN\u No)
@LabelFor(model=>model.Comment)
@EditorFor(model=>model.Comment)
您的\u Submit
控制器方法需要标记为[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);
}
});
}
函数保存附件(surveyAttachments,surveyId){
var data=new FormData();
var_files=$(surveyAttachments).prop(“文件”);
console.log(_文件);
对于(i=0;i<\u files.length;i++){
数据。追加(调查ID,_文件[i]);
}
控制台日志(数据);
$.ajax({
类型:“POST”,
url:_URLSaveAttachments,
数据类型:“json”,
数据:数据,
contentType:false,
processData:false,
成功:功能(响应){
},
故障:功能(响应){
//警报(response.responseText);
},
错误:函数(响应){
//警报(response.responseText);
}
});
}
您没有提交任何CommentModel
属性,这可能就是为什么null
@James这是什么意思?我如何发送它?表单中没有要发布的字段,我正在发布答案。表单中有字段,请看\u CommentFormAh等等,我以为问题是您没有提交