gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:53:06 27 4
gpt4 key购买 nike

我尝试设置一个小型演示,其中一篇文章有​​多个评论。文章详细信息 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com