c# - 为什么 PartialView 一直在调用自己?

标签 c# asp.net asp.net-mvc

我尝试设置一个小型演示,其中一篇文章有​​多个评论。文章详细信息 View ,应在部分 View 中呈现评论。 partialView 本身包含另一个用于添加新评论的局部 View 。

当我尝试添加另一条评论时,我收到了 InsufficientExecutionStackException,因为 Controller 中的操作不断调用自身。为什么会这样?

(如果有人手头有类(class) Material 。类似的例子应该在 Msft 的 70-486 类(class)的模块 9 中;这就是我试图构建的内容。)

编辑:完整代码在github

Edit2: Github 上的示例已修复。正如 Stephen Muecke 指出的那样,GETPOST 方法同名导致了循环引用。 在更多人指出之前,缺少 DI 和 View 模型,并且重新呈现所有评论是次优的:是的,我知道,不,那些东西都不是我想要完成的。这只是一个快速的 n 肮脏的演示。

Controller :

[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 中的相关行:

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

_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:

@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 部分,其中又包含 @Html.Action("_CreateCommentForArticle", ...)

Details() 的初始 GET 方法中, View 将正确呈现,但是当您提交表单时,当前对 _GetCommentsForArticle 页面的请求是一个 [HttpPost] 方法,因此 @Html.Action() 将查找 [HttpPost] 方法(不是 [HttpGet] 方法)。 [HttpPost] 依次呈现 _GetCommentsForArticle.cshtml 部分,并再次调用 _CreateCommentForArticle() POST 方法呈现 _GetCommentsForArticle。 cshtml partial 等等,直到内存不足并抛出异常。

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

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

并修改表格以适应

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

关于c# - 为什么 PartialView 一直在调用自己?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40771972/

相关文章:

c# - ASP.NET MVC4 : An attribute argument must be a constant expression , 属性参数类型的typeof表达式或数组创建表达式

c# - 自定义 DataAnnotationsModelMetadataProvider 不起作用

c# - 代码优先更改桥实体表的名称

c# - 后台 worker ,不运行进度条

javascript - 无法解释的 JavaScript 行为 - Bug?

asp.net - 外部AppSettings文件未与web.config合并

c# - 在代码中设置 Far Future Expires header - ASP.NET

asp.net - 授权与 Active Directory .NET Core 1.1、.Net Framework 的属性交互

c# - ASP.NET MVC 3 显示模板问题

c# - CommandLineParser - 值不变