- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我尝试设置一个小型演示,其中一篇文章有多个评论。文章详细信息 View ,应在部分 View 中呈现评论。 partialView 本身包含另一个用于添加新评论的局部 View 。
当我尝试添加另一条评论时,我收到了 InsufficientExecutionStackException
,因为 Controller 中的操作不断调用自身。为什么会这样?
(如果有人手头有类(class) Material 。类似的例子应该在 Msft 的 70-486 类(class)的模块 9 中;这就是我试图构建的内容。)
编辑:完整代码在github
Edit2: Github 上的示例已修复。正如 Stephen Muecke 指出的那样,GET
和 POST
方法同名导致了循环引用。在更多人指出之前,缺少 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/
我有一个在 Android 市场上相当流行的应用程序,它允许数以万计的用户按下一个按钮并向它发出语音命令。然后我就可以做很多不同的事情,比如给他们提供当前的天气预报等等...... 无论如何,我的应用
令人惊讶的是,标题基本上解释了它。我们有一个我们的客户制作的页面,我们正在重新创建该页面。 页面高度会一直增加,直到(我假设是这样)浏览器达到它的极限。我已经尝试过 Firebug 和 W3 验证器,
我是 react-native 的新手,试图创建我自己的组件,但它一直显示一个空屏幕。 这是我的组件代码 class BoxComponent extends Component { cons
我正在为我的 PHP 元素创建一个非常简单的博客,但遇到了一个简单的问题。我无法让我的页眉图像一直 float 。我有一个横幅,左边有一些文字,我有一个 1px 的切片,在可以选择的任何分辨率的宽度上
为什么我可以在另一个 Controller 的 View 中访问一个 Controller 的辅助方法?有没有办法在不破解/修补 Rails 的情况下禁用它? 最佳答案 @George Schreib
我正在使用带有最新 ADT 插件的 Eclipse Kepler SP2。每隔一分钟 Eclipse 就会说“为 Android 4.4.2 加载数据”并阻止我想做的一切。我在不同的文件夹中有几个 E
我是一名优秀的程序员,十分优秀!