Manoz
Manoz

Reputation: 6587

Searching in asp.net mvc4

I am newbie to asp.net MVC4. For searching names from list i tried a search filter in MVC4.

This is controller-

public ActionResult SearchUser(string Email, int? UserId) {
            var system = from u in db.SystemUsers
                         select u;
            if (!String.IsNullOrEmpty(Email)) {
                system = system.Where(c => c.Email.Contains(Email));
            }


            return View(system.Where(x=>x.Email==Email));
        }

View-

<input type="text" id="search-User" />
    <button  id="text-email">search</button>

Ajax handling-

 <script type="text/javascript">
                $(document).ready(function () {
                    $('#text-email').click(function () {
                        var areavalue = $('#search-User').val();
                        alert(areavalue);
                        $.ajax({
                            url: '/Allusers/SearchUser/?Email=' + areavalue,
                            type: 'get',
                            datatype: 'json'
                        });
                    });
                });
</script>

ViewModel-

public class UserModel
    {
        [Required]
        public string Email { get; set; }

        public int UserId { get; set; }
    }

I have many users as a list, so i wanted to filter out any user from the list. For this I am using input element to get exact name as it is in list. Thus this name is passed to controller to find the exact match.

It is showing value i passed through ajax handling but not showing filtered result.

How can I perform searching in Asp.net MVC4?

Upvotes: 1

Views: 2980

Answers (3)

Andrey Gubal
Andrey Gubal

Reputation: 3479

I would use better Load() function for this porpose:

 <script>
   $(function () {
   $('#text-email').click(function () {
       var areavalue = $('#search-User').val();
       $(".YourDivForResults").Load('/Allusers/SearchUser/?Email=' + areavalue)                       
                    });
                });
</script>

And, as a recommendation, modify a bit your ActionResult as follows:

system = system.Where(c => c.Email.ToUpper().Trim().Contains(Email.ToUpper().Trim()));

This way you will avoid problems with empty spaces and Upper or Low letters.

Upvotes: 1

Yaakov Ellis
Yaakov Ellis

Reputation: 41490

Your ajax function is sending the data up to the server, but it is not doing anything with the result. In order to use the results, you should use the done promise method in the jQuery .ajax method that you are calling. It will look something like this:

$.ajax({
  url: '/Allusers/SearchUser/?Email=' + areavalue,
  type: 'get',
  datatype: 'json'
}).done(
        function(data, textStatus, jqXHR) {
          var object = jQuery.parseJSON(data);
          // LOGIC FOR UPDATING UI WITH RESULTS GOES HERE
        }
);

You could alternatively use the Success callback option (instead of done). But the key is to provide logic for what to do with the data returned by the Action.

Also, if you are intending to return results using your ViewModel, you may need to return UserModel objects from your Linq query.

And if you are expecting JSON back from your action, you should not return View. Instead, try returnins JSON(data). (See here for more).

Upvotes: 1

Karthik Bammidi
Karthik Bammidi

Reputation: 1851

You need to make small change to you action like,

public ActionResult SearchUser(string Email, int? UserId) {
            var system = from u in db.SystemUsers
                         select u;
            if (!String.IsNullOrEmpty(Email)) {
                system = system.Where(c => c.Email.Contains(Email));
            }


            return Json(system.Where(x=>x.Email==Email),JsonRequestBehavior.AllowGet);
        }

and in your ajax call

 $(document).ready(function () {
                        $('#text-email').click(function () {
                            var areavalue = $('#search-User').val();
                            alert(areavalue);
                            $.ajax({
                                url: '/Allusers/SearchUser/?Email=' + areavalue,
                                type: 'get',
                                datatype: 'json',
    success:function(data){JSON.stringify(data);}
                            });
                        });


                });

so that you will get the search result in a json format. you can make use of it. Hope it helps

Upvotes: 0

Related Questions