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

时间:2013-12-17 15:12:30

标签: c# asp.net-mvc

我有以下控制器,它适用于_ 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>

查看 - _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>

我尝试了所有可能的事情,但找不到解决方法。我错过了什么?

修改 小提琴出来:

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>

3 个答案:

答案 0 :(得分:1)

我认为问题是你在部分视图中传递了不同的模型。 您需要创建一个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 :(得分:1)

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

答案 2 :(得分: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);
                }
            });
        }