Click Here For MCQ

Wednesday, June 10, 2020

Ascending and Descending order by single button in ASP.net MVC C#

           Controller   


        public ActionResult Index(string search , string Sortorder , string Sortby)
        {         
            // we send list to our view by model employee
            var employees = GetEmployees(); 

            // this code is for when search button press lets run 
            if (search != null)
            {   
                var  model = employees.Where(x => x.Name.Contains(search) || x.Email.Contains(search)).ToList();
                return View(model);
            }


            // for  ascending and descending order
            ViewBag.sortorder = Sortorder;
            switch (Sortorder)
            {
                case "Acs" :
                    {
                        employees = employees.OrderBy(x => x.Name).ToList();
                        break;
                    }
                case "Des":
                    {
                        employees = employees.OrderByDescending(x => x.Name).ToList();
                        break;
                    }
                default:
                    {
                        employees = employees.OrderBy(x => x.Name).ToList();
                        break;
                    }
            }


                return View(employees);
        }



 private List<Employee> GetEmployees()
        {
            return new List<Employee>()
            {
                new Employee() {Id=1, Name="Ritesh" ,Email="ktm@123.com" },
                new Employee() {Id=2, Name="Rahul" ,Email="ptm@123.com" },
                new Employee() {Id=3, Name="suresh" ,Email="ptm@123.com" },
                new Employee() {Id=4, Name="zhaigham" ,Email="ptm@123.com" },
                new Employee() {Id=5, Name="zaheer" ,Email="ptm@123.com" },
                new Employee() {Id=6, Name="tarun" ,Email="ptm@123.com" },
                new Employee() {Id=7, Name="prateek" ,Email="ptm@123.com" },
                new Employee() {Id=8, Name="lotan" ,Email="ptm@123.com" },
                new Employee() {Id=9, Name="karma" ,Email="ptm@123.com" },
                new Employee() {Id=10, Name="pooja" ,Email="ptm@123.com" },
                new Employee() {Id=11, Name="bablu" ,Email="ptm@123.com" },
                new Employee() {Id=12, Name="suraj" ,Email="ptm@123.com" },
                new Employee() {Id=13, Name="anamika" ,Email="ptm@123.com" },
                new Employee() {Id=14, Name="prayag" ,Email="ptm@123.com" },
                new Employee() {Id=15, Name="Rohit" ,Email="mtm@123.com" }
            };
        }


Model


 public class Employee
    { 
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }



View





@model IEnumerable<MyjavaProg.Models.Employee>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<form> 
    <input type="text" name="search" />
    <button type="submit" name="type for search">submit</button>
</form>

<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Name","Index",new { Sortorder= ViewBag.sortorder==null ? "Asc" : (ViewBag.sortorder=="Asc" ? "Des" : "Asc"), Sortby="Des"})
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>





No comments:

Post a Comment