SIGN UP MEMBER LOGIN:    
ARTICLE

Autocomplete Textbox in Razor View Engine Using JQuery in ASP.NET MVC 3

Posted by Shirsendu Nandi Articles | ASP.NET MVC with C# January 30, 2012
In this article I will explain how to use an Autocomplete textbox in razor using Jquery.
Reader Level:

Suppose we are building a contact group application in ASP.NET MVC3 using razor view engine.

Here one contact group can hold multiple contact information. That means it is a 1 to many relationship.

Now we have 2 model classes.

  1. Group
  2. Contact

Under the controller folder we have one controller class named "groupcontroller.cs".

Now under the "view" folder we have a different view.

Now we have to deal with "create.cshtml".

Here while typing your group name it will automatically show a possible group name like {friend,family,twiter friend,company e.t.c}.

So for that first we have to include the Jquery in our "create.cshtml" file like in the following code:

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<link href="../../Content/themes/base/jquery.ui.autocomplete.css" rel="stylesheet"
    type="text/css" />
<script src="../../Scripts/jquery-ui-1.8.11.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script>

Now in the razor view engine we are displaying and taking the group name value like in the following code segment:
 
<fieldset>
      <b><font color="red">Enter Your Group Name on which you will insert your Contact Information</font></b>
      <div class="editor-label" >
            @Html.LabelFor(model => model.Group.GroupName)
      </div
>
      <div  class="editor-field">
            @Html.EditorFor(model => model.Group.GroupName)
            @Html.ValidationMessageFor(model => model.Group.GroupName)
      </div
>
</fieldset>

See the "            @Html.EditorFor(model => model.Group.GroupName)

Now we have to write a Jquery with dealing @Html.EditorFor

Please see the following JavaScript section code:

<script type="text/javascript">
       $(function() {
           var availableTags = [
            "Buisness",
            "College Friend",
                     "Colligue",
                     "Company",
            "Friend",
                     "Family",
                     "Personal",
            "Achool Friend",                   
                     "Relative",               
                     "FacebookFriend",
                     "Twiter Follower",
            "Others"
              ];      
$("#Group_GroupName").autocomplete({ 
    source: availableTags
}); 
});
</script>

See here first I have stored all my possible groupnames in an "
availableTags" variable.

After that under the $ section I have written the id for the "
@Html.EditorFor(model => model.Group.GroupName)

So here the id will be "
Group_GroupName".

So after that while running the application we will see like the following result.

mvc3.gif

See in the above picture while typing a letter "a" it is displaying all possible letters.

See here I have used a hard-coded value; we can get that value from a database itself and with dealing with Jquery we can populate the Autocomplete extender textbox.

Conclusion : So in this article we got to know how to use an Autocomplete textbox in razor view engine using Jquery.

Login to add your contents and source code to this article
share this article :
post comment
 

This needs to be improved. What you are showing here is no more than the simple examples on the jquery website. This needs to show an ajax call to a controller action and returning json to the browser. This example is just a simple, local script.

Posted by Steven Chalk Mar 11, 2012

Its a very good and useful article.

Posted by Adora Krause Jan 31, 2012

Thank you for sharing

Posted by Sonakshi Singh Jan 31, 2012
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor