为什么PartialView不断调用自身? [英] Why does the PartialView keep calling itself?

查看:95
本文介绍了为什么PartialView不断调用自身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试建立一个小型演示,其中的一篇文章中有多个评论.文章详细信息视图应在部分视图中呈现注释. partialView本身包含另一个用于添加新注释的局部视图.

I've tried to set up a small demo, in which an article has multiple comments. The article details view, should render the comments in a partial view. The partialView itself contains another partial view for adding a new comment.

当我尝试添加另一个注释时,我收到一个InsufficientExecutionStackException,因为控制器中的动作一直在自我调用.为什么会这样?

When I try to add another comment I receive a InsufficientExecutionStackException, because the action in the controller keeps calling itself. Why does this happen?

(如果有人手头准备了课程材料.类似的示例应该来自Msft的70-486课程的第9单元;这就是我要尝试的内容.)

(If somebody has the course material at hand. A similar example should be at module 9 in the 70-486 course from Msft; that's what I trying to build.)

完整代码位于 github

Edit2 :Github上的示例已修复.正如斯蒂芬·穆克(Stephen Muecke)所指出的,GETPOST方法名称相同的事实导致了循环引用. 在没有更多人指出之前,缺少了DI和View模型,并且重新渲染所有注释都不是最佳选择:是的,我知道,不,那些事情我想完成.这只是一个快速的肮脏演示.

The sample on Github has been fixed. As Stephen Muecke pointed out, the fact, that both the GET and POST method hat the same names was causing the circular reference. Before any more people point out, that DI and View Models are missing and that re-rendering all comments is sub-optimal: Yes I am aware and no, those things were nothing, that I wanted to accomplish. This was just a quick n dirty demo.

控制器:

[ChildActionOnly]
public PartialViewResult _GetCommentsForArticle(int articleId)
{
    ViewBag.ArticleId = articleId;
    var comments = db.Comments.Where(x => x.Article.ArticleId == articleId).ToList();
    return PartialView("_GetCommentsForArticle", comments);
}


public PartialViewResult _CreateCommentForArticle(int articleId)
{
    ViewBag.ArticleId = articleId;
    return PartialView("_CreateCommentForArticle");
}

[HttpPost]
public PartialViewResult _CreateCommentForArticle(Comment comment, int articleId)
{
    ViewBag.ArticleId = articleId;
    comment.Created = DateTime.Now;
    if (ModelState.IsValid)
    {
        db.Comments.Add(comment);
        db.SaveChanges();
    }
    var comments = db.Comments.Where(x => x.Article.ArticleId == articleId).ToList();
    return PartialView("_GetCommentsForArticle", comments);
}

Details.cshtml中与文章相关的行:

relevant line in the Details.cshtml for Article:

@Html.Action("_GetCommentsForArticle", "Comments", new { articleId = Model.ArticleId})

_GetCommentsForArticle:

_GetCommentsForArticle:

@model IEnumerable<Mod9_Ajax.Models.Comment>
<div id="all-comments">
    <table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Text)
            </th>
        </tr>

        @foreach (var item in Model)
        {
           @* ... *@
        }
    </table>
</div>
@Html.Action("_CreateCommentForArticle", "Comments", new { articleId = ViewBag.ArticleId })

_CreateCommentForArticle:

_CreateCommentForArticle:

@model Mod9_Ajax.Models.Comment
@using (Ajax.BeginForm("_CreateCommentForArticle", "Comments", new AjaxOptions
{
    HttpMethod = "POST",
    InsertionMode = InsertionMode.Replace,
    UpdateTargetId = "all-comments"
}))
{
    @* ... *@

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

推荐答案

要解释发生了什么,您有一个表单可以发布给您_CreateCommentForArticle()方法,然后该表单将呈现您的_GetCommentsForArticle.cshtml部分,而该部分又包含.

To explain what is happening, you have a form that posts to you _CreateCommentForArticle() method, which then renders your _GetCommentsForArticle.cshtml partial which in turn includes @Html.Action("_CreateCommentForArticle", ...).

Details()的初始GET方法中,视图将正确呈现,但是当您提交表单时,当前_GetCommentsForArticle页面的请求是[HttpPost]方法,因此@Html.Action()将查找[HttpPost]方法(不是[HttpGet]方法).该[HttpPost]依次呈现_GetCommentsForArticle.cshtml部分,并再次调用_CreateCommentForArticle() POST方法,该方法呈现_GetCommentsForArticle.cshtml部分,依此类推,直到耗尽内存并引发异常.

In the initial GET method for Details() the view will be rendered correctly, but when you submit the form, the current request for the _GetCommentsForArticle page is a [HttpPost] method, so @Html.Action() will look for a [HttpPost] method (not the [HttpGet] method). That [HttpPost] in turn renders the _GetCommentsForArticle.cshtml partial and again calls the _CreateCommentForArticle() POST method which renders the _GetCommentsForArticle.cshtml partial and so on until you run out of memory and the exception is thrown.

例如,您可以通过更改POST方法的名称来解决此问题

You can solve this by changing the name of the POST method, for example

[HttpPost]
public PartialViewResult Create(Comment comment, int articleId)

并修改表格以适合

@using (Ajax.BeginForm("Create", "Comments", new AjaxOptions { ...

这篇关于为什么PartialView不断调用自身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆