0

I am developing in asp.net mvc and today I saw some strange thing in using ajax helpers.

If I use Ajax.BeginForm(...) or Ajax.ActionLink(...), they do not call (post to) the actions asynchronously, and they work as they are normal form or normal link, but when I check Request.IsAjaxRequest() in action, it returns true.

For ajax forms, I use

$('#createqfrm').submit(function () {...}

and it works fine, and send the form asynchronously.

Note: I know I have to change .live() to .on() in jquery.unobtrusive-ajax.min since new versions of jquery.

also here are my referenced java libs:

 <script src="@Url.Content("~/Scripts/jquery-1.9.0.min.js")" type="text/javascript"></script>
     <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

Edited:

Checking Request.IsAjaxRequest() and saw that it returns false.

Action Code Added:

public ActionResult GetStriing()
        {
            if (Request.IsAjaxRequest())
            {
                return Json("ajax called",JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json("ajax not called", JsonRequestBehavior.AllowGet);
            }

        }

part of html code for form:

<form action="/admin/xxxx/create" data-ajax="true" data-ajax-success="handeCreateQuestionnareSuccess" id="form0" method="post">        <div class="editor-label">
            <label for="Title">title</label>
        </div>
        <div class="editor-field">
            <input class="text-box single-line" data-val="true" data-val-required="*" id="Title" name="Title" type="text" value="" />
            <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span>
        </div>
        <div class="editor-label">
            <label for="Status">Status</label>
        </div>
....

 <script type="text/javascript">

        function handeCreateQuestionnareSuccess(context) {
            debugger;
            if (context) {

                $("#CreateQuestionnarieform").empty().html(context[1]);
                $("#CreateQuestionnarieform").append('<input type=hidden name=questionnarieid value=' + context[2] + '/>');

            } else {

                $("#CreateQuestionnarieform").empty().html(context[1]);

                $("form").removeData("validator");
                $("form").removeData("unobtrusiveValidation");
                $.validator.unobtrusive.parse("form");
            }
        }
    </script>

Code that submits form with ajax call:

$('#createqfrm').submit(function () {

                if ($(this).valid()) {
                    $.ajax({
                        url: this.action,
                        type: this.method,
                        data: $(this).serialize(),
                        success: function (context) {



                        }
                    });
                }
                return false;
            });

not a solution but can notify other users:

The problem is with jquery 1.9.0 and updates that I get from nuget for jquery.validate.min.js and jquery.validate.unobtrusive.min.js I changed back to 1.8.3 and it works fine.....

Babak Fakhriloo
  • 2,076
  • 4
  • 44
  • 80

1 Answers1

1

Well that sounds like UnobtrusiveJavaScriptEnabled is set to false. Have a look in your AppSettings-File. In section <appsettings> there should be:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

EDIT:

Just found this link after a minute of googling:

http://forums.asp.net/t/1877166.aspx/1?jquery+unobtrusive+ajax+js+error+with+jQuery+1+9+0+updated

Unobtrusive Ajax stopped working after update jQuery to 1.9.0

Community
  • 1
  • 1
iappwebdev
  • 5,880
  • 1
  • 30
  • 47