Asp.net mvc PartialView呈现新页面,而不是在div中

Asp.net mvc PartialView呈现新页面,而不是在div中,asp.net-mvc,Asp.net Mvc,我以前有过这个问题,我一辈子都记不起如何解决它 或者我在谷歌上搜索的地方 我从一个动作中回到了一个局部的观点 return PartialView("CommentsListControl", recipe.Comments); 但这会加载一个新网页,而不会加载到我的div中为什么 <table width="100%"> <tr><td> <div id="divComments">

我以前有过这个问题,我一辈子都记不起如何解决它 或者我在谷歌上搜索的地方

我从一个动作中回到了一个局部的观点

        return PartialView("CommentsListControl", recipe.Comments);
但这会加载一个新网页,而不会加载到我的div中为什么

<table width="100%">
    <tr><td>
        <div id="divComments">
            <% using (Ajax.BeginForm("CreateComment", new AjaxOptions { UpdateTargetId = "divCommentsList", InsertionMode = InsertionMode.Replace }))
               { %>
            <textarea id="comments" name="comments" rows="3" cols="60"></textarea>
            <table width="100%"><tr><td align="right">
                <a href="#" id="savecomment">Save Comment</a>
                <%= Html.Hidden("recipeid", Model.RecipeID) %>
            </td></tr>
            </table>
            <% } %>
        </div>
    </td></tr>
    <tr><td>
        <div id="divCommentsList">
            <% Html.RenderPartial("CommentsListControl", Model.Comments); %>
        </div>
    </td></tr>
</table>

您展示的样本在表单上没有提交按钮。在WebForms中,您可以使用“asp:Hyperlink”控件生成一个链接,该链接看起来就像它提交了一个表单一样,但这是因为WebForms为您实现了JavaScript。在MVC中,你必须自己做。尝试将“保存评论”链接换成“提交”按钮:

<input type="submit" value="Save Comment" />


然后使用CSS根据您的心意设置按钮样式。

表单正在使用JQuery提交。但您仍然是对的。submit按钮起作用JQuery submit并不期望JQuery使用.submit()方法来提交表单,这会跳过由AJAX.BeginForm助手呈现的AJAX代码。