SIGN UP MEMBER LOGIN:    
ARTICLE

Sorting and Searching in ASP.NET MVC Application

Posted by Prashant Chaudhary Articles | ASP.NET MVC with C# November 30, 2011
In this this article you will learn how to use the Sorting and Searching functionality in an ASP.NET MVC Application.
Reader Level:

Functionality of Sorting and Searching in ASP.NET MVC Application

While developing any application, the user should use many types of useful functions among them, Sorting and Searching are very important and widely used functions in any method of an application.

Here I am showing Sorting and searching in a table named MCN having columns Name and Age.  

First develop the database table as shown in the below figure:

DB1.gif


db2.gif

Now develop an application in ASP.NET MVC in which you can use this table and retrieve the data.

nw2.gif

Click on the Student Hyperlink to retrieve data from the table to the grid. Here i am not showing the database connection procedure.

nw1.gif

To see the use of Sorting in columns, create an MCN controller(which you have add to the controller) and manipulate the code.

The code is as follows, which will be done in Controllers\MCNController.cs (Index method)

public ViewResult Index(string sortOrder)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name asc" : "";
ViewBag.AgeSortParm = sortOrder == "Age" ? " desc" : "Age";
var mcn = from p in db.MCN
select p;
switch
(sortOrder)
{
    case
"Name Asc":
    mcn= mcn.OrderByDescending(p => p.Name);
           break;
           case
"Age":
            mcn = mcn.OrderBy(p => p.Age);
            break;
           case "Age desc":
   mcn = mcn.OrderByDescending(p => p.Age);
   break;
   default:
      mcn = mcn.OrderBy(p => p.Name);
         break;
   }
    return View(mcn.ToList());
}

Names in Ascending Order:

nw3.gif

And Ages in Descending Order:

nw4.gif  

In the above code, the sorting function has been applied to the  Name and Age columns which are present in the database named MCN.db. Now to show the Search Function in ASP.NET MVC.

Create a Textbox in which you will write the string which you want to search.  
Now code it to show this functionality that will be:

if (!String.IsNullOrEmpty(searchString))
{
    mcn = mcn.Where(p => p.Name.ToUpper().Contains(searchString.ToUpper())
}

After that code the View page of MCN/Index.cshtml

 <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
    }
</script>
<
asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
    MCN
</asp:Content>
<
asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>
    @using (Html.BeginForm())
{
    <p>
        Find by name: @Html.TextBox("SearchString") &nbsp;
        <input type="submit" value="Search" /></p>
}<table style="width: 100%">
        <tr>
            <td style="width: 102px">
                <asp:Button ID="Button4" runat="server" Text="Search" />
            </td>
            <td style="width: 269px">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
            <td>
                &nbsp;</td>
        </tr
>

nw5.gif

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor