Submitting AjaxForm using jQuery in ASP.NET MVC

I have an ajax form in asp.net mvc that is simple:


    &lt% using (this.Ajax.BeginForm("LatestBlogPosts", "Blog", null, new AjaxOptions { UpdateTargetId = "blogPostPanel" }, new { id = "BlogPostForm" })) { %>
    <div class="panel" id="blogPostPanel">
        <img src="/images/ajax-loader.gif" alt="ajax-loader" />
    </div&gt
    <% } %>

I want to trigger a form submission when loading a document. Presumably, this should trigger a controller action and return a result that should be replaced with a DIV placeholder. If I add the SUBMIT button to the form, it works fine, but when I call the submission via jQuery, the whole page is updated, and the content returned by the server is displayed on the newly displayed page. Here is my jQuery code:


    <script type="text/javascript">
        $(document).ready(function() {
            $("#BlogPostForm").submit();
        });
    </script>

Anyway to do this?

+3
source share
2 answers

, , , , jQuery.

<div class="panel" id="blogPostPanel">
    <img src="/images/ajax-loader.gif" alt="ajax-loader" />
</div>

<script type="text/javascript">
    $(function() {
         $('#blogPostPanel').load( '<%= Url.Action( "LatestBlogPosts", "Blog" ) %>' );
    });
</script>
+2

Source: https://habr.com/ru/post/1708329/


All Articles