部分视图 - 控制器接收到空对象

3

我有以下控制器,对于 _GetForSessionCommentForm 控制器,它们可以正常工作。但是当点击 _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输出:

fidler

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>

您没有提交任何“CommentModel”属性,这可能是为什么它为“null”的原因。 - James
@James,你是什么意思?我该怎么发送它? - DevT
表单内有字段,请查看 _CommentForm。 - Maess
等等,我以为问题是你没有渲染“_CommentForm”,但我刚意识到它是一个子操作...你能否使用HTTP调试器(如[Fiddler](http://fiddler2.com/))检查请求并查看字段是否被发布? - James
@James 当我尝试运行Fiddler时,它没有捕获请求。 - DevT
显示剩余3条评论
3个回答

1
我认为问题在于您在部分视图中传递了不同的模型。您需要创建一个ViewModel,然后将相同的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>

_CommentForm(评论表单)
@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" />

1

您的_Submit控制器方法需要标记为[HttpPost]。否则,它将无法读取被提交表单中的数据。


1
没问题。[HttpPost] 是一件容易被忘记的事情,但绝对是让数据离开页面必不可少的。我粗略地查看了 Lin 的答案,没有看到它,所以我想确保你也知道你需要它。 - krillgar

0
不要在传递之前将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);
                }
            });
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接