Why does the PartialView keep calling itself?

后端 未结 1 949
天涯浪人
天涯浪人 2020-12-11 05:55

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

相关标签:
1条回答
  • 2020-12-11 06:18

    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", ...).

    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.

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

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

    and modify the form to suit

    @using (Ajax.BeginForm("Create", "Comments", new AjaxOptions { ...
    
    0 讨论(0)
提交回复
热议问题