Skip to main content

ASP.Net MVC 4 + jQuery Autocomplete + Json

Tools used
1. Microsoft VS 2012
2. Jquery
3. Jquery-ui
4. Jquery-ui.css

Step to integrate – ASP.Net MVC 4 + jQuery Autocomplete + Json

  1. 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.
  2. 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.
    Autocomplete.
  3. Download jQuery-ui.css and save into content folder.
  4. Create a controller named JqueryController. (Right click on Controllers Folder > Add > Controller)
  5. In the JqueryController.cs, add a view with name index. (Right click on ActionResult > Add View)
  6. Go to App_start>BundleConfig.cs, and modify your code to include the download jquery-ui.css
  7. 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
    1. 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.
    To test the created function, browser to this URL http://yourip/jquery/autocomplete?search=, you will have this result return in your browser.
    Autocomplete
    1. In the Jquery/Index.chtml. Copy code from jQuery website and modify a little bit to make it look like MVC Code :P
    Please take note the @Url.Action("AutoComplete") will generate url string in this format ‘/contoller/action/’ i.e. it will generate /jquery/autocomplete’
    1
    1. 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") 
      Autocomplete
    2. Run the application (F5). That’s all!
      Autocomplete

Comments

Popular posts from this blog

FlexBox (combobox+json +Paging)

FlexBox Visit the FlexBox Home Page FlexBox  is a jQuery plugin that is intended to be a very flexible replacement for html textboxes and dropdowns, optionally using ajax to retrieve and bind JSON data. It can be used as a: ComboBox, with optional per-result html templates Suggest box, like Google's search Data-driven type-ahead input box It supports: Auto-completion using local (JSON) or remote (JSON via ajax) data Skinning via css Flexible paging Configurable client-side caching Much more... (see Configuration Options in the documentation) Screenshotflex More demos and code examples

ASP .NET - XML Files

An XML File Here is an XML file named "countries.xml": <?xml version="1.0" encoding="ISO-8859-1"?> <countries> <country>   <text>Norway</text>   <value>N</value> </country> <country>   <text>Sweden</text>   <value>S</value> </country> <country>   <text>France</text>   <value>F</value> </country> <country>   <text>Italy</text>   <value>I</value> </country> </countries> Bind a DataSet to a List Control First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of an .aspx page: <%@ Import Namespace="System.Data" %> Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded: <script runat="server"> sub Page_Load if Not Page.IsPos...

SQL JOINS &UNION

SQL JOIN The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables. Tables in a database are often related to each other with keys. A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table. Look at the "Persons" table: P_Id LastName FirstName Address City 1 Hansen Ola Timoteivn 10 Sandnes 2 Svendson Tove Borgvn 23 Sandnes 3 Pettersen Kari Storgt 20 Stavanger Note that the "P_Id" column is the primary key in the "Persons" table. This means that  no  two rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same name. Next, we have the "Orders" table: O_Id OrderNo P_Id 1 77895 3 2 44678 3 3 22456 1 4 24562 1 5 34764 15 Note that the "O_...