Handling Text Changed Event In ASP.NET MVC 3 With JQuery

16. July 2011 13:00

ASP.NET MVC Jquery  4 Comments

In my quest of learning MVC I came across many problems and here is one of them. I was trying to fire an event whenever a text changed event occurred. I am using JQuery to handle the text changed event but eventually the text changed event doesn't seems to be working in MVC as it is working for a text box changed event in web forms. The solution I found seems to be working fine for me as I don't want to fire an event on every key press.

To get started fire Visual Studio and create a new MVC 3 application with RAZOR view engine. I have build a simple contact manager application which allows the user to create a new contact, edit and delete existing contacts. While creating a new contact, what I want is when the user enters the e-mail address of the contact I want to make an AJAX call, which then call the method in the controller and return the HtmlString which will then show the Gravatar on the webpage.

Here is the default view of the index view. I will ask the user to enter some very basic information like name, address, email and phone.

Now, when the user enters the email address of the contact and press TAB key to move to another field or just click somewhere else on the page, the focus on the text field will be changed and the controller function then returns HtmlString to show the Gravatar image.

Here is the markup of the Create View (Create.cshtml):

@model TextChangedMVC.Models.Contacts
    ViewBag.Title = "Create";
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            <div id="Gravatar">
            @Html.ValidationMessageFor(model => model.Email)
        <div class="editor-label">
            @Html.LabelFor(model => model.Phone)
        <div class="editor-field">
            @Html.EditorFor(model => model.Phone)
            @Html.ValidationMessageFor(model => model.Phone)
            <input type="submit" value="Create" />
    @Html.ActionLink("Back to List", "Index")

Check the highlighted code in the above markup where I have a DIV called Gravatar. This is where I will display the Gravatar of the person. Remember, when the user enters the email address we need to make sure that the Gravatar of the person displays without being an obstruction to the user, so an AJAX request to the controller will help in achieving this.

Below is the Jquery which will call the controller method called Avatar which accepts email as a parameter of string type.

<script type="text/javascript">
    $('#Email').change(function () {
        var selection = $('#Email').val();
        if (selection.length > 0) {
            $.post("/Home/Avatar", { email: selection },
        function (data) {

The above script is simple and here I am calling the controller's Avatar method which returns an HtmlString. At line 6, we are making a request and calling the controller's method. At line 8, the data (which is a HTML) returned will be set on the DIV called Gravatar (the placeholder for the Gravatar image).

The controller method Avatar is as follows:

public HtmlString Avatar(string email)
      return Gravatar.GetHtml(email, defaultImage: "http://midnightprogrammer.net/pics/default.gif");

Gravatar is a method is a part of the Microsoft Web Helpers. You can use web helpers by downloading it via NuGet. Check it out here if you miss my blog post on how to use Microsoft Web Helpers in MVC.

Related Post:

Currently rated 3.4 by 9 people

Comments (4)

Ali Ali
12/17/2015 7:37:29 AM #

Hi, I do everything that u say but the code did not work!

Prashant Prashant
12/19/2015 2:58:14 PM #

@Ali: Can you post the error you get in the browser console.

Savitri Shirshad Savitri Shirshad
4/9/2016 12:38:11 PM #

Hi, Thank you for the code, But it didnot work for me. I am not getting any error but it is not passing the Textbox data to Controller method. Could you help me in this regard, Thank you in advance :)

Prashant Prashant
5/14/2016 5:11:36 PM #

@Savitri Shirshad: Looking at your comment I can say that there is a problem with the routes. Can you share the code with me so I can see where the problem is. Also did you try checking the routes in your application?

Add Comment

Visit blogadda.com to discover Indian blogs Computers Blogs