Asp.net mvc Ajax. BeginForm cannot be refreshed asynchronously, or switched to a new page, or the page is refreshed.
I made an afternoon of Ajax yesterday. beginForm failed to Implement Asynchronous refresh, and the returned data was always submitted to a new page. I searched for n methods on the Internet, but I asked n people to solve the problem. This is the first day of the year, I was surprised to find the problem.
Layout page:
<div id="upload-body"> <div id="returnData"><script type="text/ecmascript"> function Reload(data) { alert(data); } function DisplayError(data) { alert(data); }</script>
Introduce jquery. unobtrusive-ajax.js: <script src = "~ /Scripts/jquery. unobtrusive-ajax.js "> </script>
Control:
[HttpPost] public JsonResult Index(Models.TestModels.TestModel testModel) { return Json("Hi,"+testModel.UserName, JsonRequestBehavior.AllowGet); }
Then there is a problem:
After submission:
This is not the expected result. The good result is that the page will not be refreshed or jump to the page. Then start to look up the problem and see if jquery. unobtrusive-ajax.js is referenced,
I did reference it, and I went online to find it. I did not have any resources available. I tried all kinds of methods and still did not solve the problem. Alas, is it Ajax. beginForm only has its own name in vain. In my heart, I am quietly swearing: % $ junk % $! # ¥ %...
Failed to return last night.
I came to the company this morning and opened it with Chrome again. I was surprised to find that:
In an instant, I felt a little furious and excited. Is it because of this? No, are you sure you have referenced it?
After carefully observing the last and third images, we were surprised to find that jquery was behind jquery. unobtrusive-ajax,
The original reference of _ Layout. cshtml is at the bottom. Hey hey, so I decided to directly put the reference of jquery. unobtrusive-ajax after jquery,
Run again:
Haha, the effect is really good. Suddenly, I can't help but have a great reverence and admiration for Ajax. BeginForm. It's really a waste!
Conclusion: Pay attention to the reference sequence of jQuery and other js.