Ajax.ActionLink and Ajax.BeginForm not working in Asp.net MVC
In Asp.net MVC Ajax Request like ActionLink and BeginForm not worked many times and we faced problems like Ajax Request refresh whole page or replace page with partial view also other problems like Ajax request call controller twice and Ajax Beginform working but Ajax Actionlink not working. Here we will see all these different reasons for not working Ajax Request and also proper method to solve it.
Ajax Request refersh whole page or replace page with partial view
Here we will see step by step working solutions for Ajax.ActionLink and Ajax.BeginForm .
Web Config App Setting
These are default setting in Web config file but it is good to ensure because in Visual Studio Old version it may not given.
Add all required Jquery
Second thing we have to be ensure that we have properly added all required Jquery version to our applications. For MVC ajax request to work we have to add at least following Jueries.1234<script src="~/Scripts/jquery-1.7.1.js"></script><script src="~/Scripts/jquery.validate.js"></script><script src="~/Scripts/jquery.validate.unobtrusive.js"></script><script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> @*This is for MVC Ajax Request *@
Inspect in Browser
After adding all Jqueries, we have to also inspect in browser that all Jqueries are added successfully or not . Here I have attached Browser’s Inspection snippets.
Above I have taken snippets from google chrome. you can check in any browser of your choice.
This is very important to inspect in browser, specially here we have to check that Jquery and Jquery unobtrusive ajax are added successfully.
.live replace with .on in Jquery Unobtrusive Ajax
This could be your headache problem and you will take so much time to solve it. Usually when we start to develop our application in MVC with new project and use Internet Application in our project, we have all required jquery added in Scripts Folder. At this point of time if you have followed above instruction than it will work fine but problem will start appear when you replace Jquery old version with your latest Jquey version for your designing template.
.live method deprecated in Jquery Version 1.7 and it is completely removed in Jquery Version 1.9 and above . .live method is replaced with .on. Now if you are using jquery latest version that is 1.9 and above but you are still using Jquery Unobtursive Ajax which has old version with .live so you could have following error that is “undefined is not a function”. For solutions simply download Jquery Unobtrusive Ajax from nuget and replace with latest version .
As you can see in above snippets , .live method must be replaced with .on method. Kindly download latest Microsoft Jquery Unobtrusive ajax from nuget and you will solve it.
Note – Try your best to remvove all Html5 errors for Working Ajax request like ajax actionlink and ajax beginform in Asp.net MVC
Use Return PartialView instead of View from Action
There could be slip of mind that while we return partial view from action by mistake we use “return view()” instead of “return partialview()” even we have created partialview. So make sure you have used “return partialview()”.
Make sure that you have created a PartialView not a View
See while we use return partial view from action using “return partialview” and passing argument for partialview name at that time make sure that you have created partialview without specifying “Layout”. It must not be normal view.
Form tag inside form tag in render html for Ajax.Beginform
Html does not support form tag inside form tag. while working with Ajax.Beginform, it creates form tag. So one thing you have to check or inspect in your browser that render html does not have form tag inside form tag because it is invalid html
Ajax Request call controller twice
Sometimes we can see unexpected behavior like when we use Ajax.ActionLink and Ajax.BeginForm for calling controller and we find that Controller is going to call two times. This is because we have added both Microsoft Jquery Ajax Unobtrusive full version and Microsoft Jquery Ajax Unobtrusive minified version. so to solve this issue remove any of the Jquery version.
Ajax Beginform working but Ajax Actionlink not working
This one also can be headache sometimes that your Ajax.Beginform working perfectly while same time Ajax.ActionLink not working. In order to solve this issue use your Microsoft Jquery Ajax Unobtrusive full version and remove minified version.
Note – If possible than always use Microsoft Jquery Ajax Unobtrusive full version not minified versions to overcome with unnecessary issues.
What is your opinion ?
In this articles we have seen working solutions for Ajax.ActionLink and Ajax.Beginform. I hope it will be very useful to solve your issue regarding Ajax request in Asp.net MVC. If you have any query and question, you can comment or you can mail me.