Tools used
1. Microsoft VS 2012
2. Jquery
3. Jquery-ui
4. Jquery-ui.css
1. Microsoft VS 2012
2. Jquery
3. Jquery-ui
4. Jquery-ui.css
Step to integrate – ASP.Net MVC 4 + jQuery Autocomplete + Json
- First of all, we will assume you know how to create a new MVC project in MS 2012, in case you need guide, please refer to here.
- After create the project, By default, jquery and jQuery-UI javascript files has been created in Scripts folder and you will see files like image below, We will need these files in later step.
. - Download jQuery-ui.css and save into content folder.
- Create a controller named JqueryController. (Right click on Controllers Folder > Add > Controller)
- In the JqueryController.cs, add a view with name index. (Right click on ActionResult > Add View)
- Go to App_start>BundleConfig.cs, and modify your code to include the download jquery-ui.css
- Now we need to prepare json data source, go to Controller> JqueryController add this function, this function will search any text send from browser and then return data as jsonresult.
- In the Jquery/Index.chtml. Copy code from jQuery website and modify a little bit to make it look like MVC Code
- You need to make sure the jQuery javascript is place on top of jQuery-UI javascript file or else Autocomplete will not work. Modify _Layout.chtml, and it will look like this
@Scripts.Render("~/bundles/jquery")
- Run the application (F5). That’s all!
Notes: By adding new line into bundle meaning that all view will download the file even thought the page does not require the file, We assume that all of the view need this file, so that is why we include the file in bundleConfig.cs
To test the created function, browser to this URL http://yourip/jquery/autocomplete?search=, you will have this result return in your browser.
Please take note the
@Url.Action("AutoComplete")
will generate url string in this format ‘/contoller/action/’ i.e. it will generate /jquery/autocomplete’1
0 Comments