1

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?

Manoz
  • 6,507
  • 13
  • 68
  • 114
  • Well do you want results that contain the email or results that match it exactly ? You're doing both right now. – Dimitar Dimitrov Aug 25 '13 at 12:42
  • @DimitarDimitrov, The string i am passing in controller has exact name from the usernames. I want to show that name only. – Manoz Aug 25 '13 at 12:48

3 Answers3

1

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).

Community
  • 1
  • 1
Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
1

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.

Andrey Gubal
  • 3,481
  • 2
  • 18
  • 21
0

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

Karthik Bammidi
  • 1,851
  • 9
  • 31
  • 65