簡體   English   中英

部分視圖-控制器接收空對象

[英]Partial view- Controller receive null object

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

我嘗試了所有可能的事情,但找不到解決方案。 我在這里錯過了什么?

編輯:提琴手輸出:

小提琴手

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM