3

I have an ajax form in asp.net mvc which is as simple as this:


    <% 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>
    <% } %>

I want to invoke the form submit when document is loaded. This should supposedly, call the controller's action and return a result that should be replaced with the placeholder DIV. If I add a SUBMIT button to the form, it works perfectly, but when I invoke the submit via jQuery, the whole page is refreshed, and the content returned by the server is displayed in the newly displayed page. Here's my jQuery code:


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

Anyway to do this?

1
  • same thing happens if I submit the form by javascript only: function SubmitForm() { document.forms["BlogPostForm"].submit(); } the whole page is refreshed and the content is displayed on a new page. Commented May 14, 2009 at 10:13

1 Answer 1

2

I think it may work if you use the trigger method to generate the submit event, but I think there's a less complicated way to do this using 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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.