This question is taken from the book .NET interview question book latest edition written by Shivprasadkoirala and published by BPB publications.
You can implement Ajax in two ways in MVC:-
•Ajax libraries
•Jquery
Below is a simple sample of how to implement Ajax by using “Ajax” helper library. In the below code you can see we have a simple form which is created by using “Ajax.BeginForm” syntax. This form calls a controller action called as “getCustomer”. So now the submit action click will be an asynchronous ajax call.
In case you want to make ajax calls on hyperlink clicks you can use “Ajax.ActionLink” function as shown in the below code.
So if you want to create Ajax asynchronous hyperlink by name “GetDate” which calls the “GetDate” function on the controller , below is the code for the same. Once the controller responds this data is displayed in the HTML DIV tag by name “DateDiv”.
Below is the controller code. You can see how “GetDate” function has a pause of 10 seconds.
The second way of making Ajax call in MVC is by using Jquery. In the below code you can see we are making an ajax POST call to a URL “/MyAjax/getCustomer”. This is done by using “$.post”. All this logic is put in to a function called as “GetData” and you can make a call to the “GetData” function on a button or a hyper link click event as you want.
Below are some references for c# and .NET interview questions. Remember MVC is a small part we should be able to also answer questions around c#, .NET, SQL Server and ADO.NET sections as well.:-
• 1000 c# interview question site , awesome collection.
• This is a paid site which has collection of .NET interview question as well as answers worth of every penny.
• 20 great SQL Server interview questions and answers in codeproject.co
• 25 important WCF interview questions a must read if you have a job profile for WCF.
• Also see 20 minutes of MVC ( Model view controller ) interview questions and answer videos from youtube.com :-
