asp.net mvc ajax helper/post additional data w/o jquery.

Posted by Jopache on Stack Overflow See other posts from Stack Overflow or by Jopache
Published on 2010-03-31T06:53:15Z Indexed on 2010/04/01 22:23 UTC
Read the original article Hit count: 281

Filed under:
|
|

I would like to use the ajax helper to create ajax requests that send additional, dynamic data with the post (for example, get the last element with class = blogCommentDateTime and send the value of that last one to the controller which will return only blog comments after it).

I have successfully done so with the help of jQuery Form plugin like so:

 $(document).ready(function () {
        $("#addCommentForm").submit(function () {

            var lastCommentDate = $(".CommentDateHidden:last").val();
            var lastCommentData = { lastCommentDateTicks: lastCommentDate };
            var formSubmitParams = { data: lastCommentData, success: AddCommentResponseHandler }

            $("#addCommentForm").ajaxSubmit(formSubmitParams);
            return false;
        });

This form was created with html.beginform() method.

I am wondering if there is an easy way to do this using the ajax.beginform() helper?

When I try to use the same code but replace html.beginform() with ajax.beginform(), when i try to submit the form, I am issuing 2 posts (which is understandable, one being taken care of by the helper, the other one by me with the JS above. I can't create 2 requests, so this option is out)

I tried getting rid of the return false and changing ajaxSubmit() to ajaxForm() so that it would only "prepare" the form, and this leads in only one post, but it does not include the extra parameter that I defined, so this is worthless as well.

I then tried keeping the ajaxForm() but calling that whenever the submit button on the form gets clicked rather than when the form gets submitted (I guess this is almost the same thing) and that results in 2 posts also.

The biggest reason I am asking this question is that I have run in to some issues with the javascript above and using mvc validation provided by the mvc framework (which i will set up another question for) and would like to know this so I can dive further in to my validation issue.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about jQuery