Sunday, July 5, 2020

Simple List View in cshtml in ASP.Net MVC Pagination

Controller
https://www.facebook.com/sheeta.ba1/posts/2245132475530764
 public ActionResult Index(string Sortorder , string  Sortby,int PageNumber=1)
        {
            ViewBag.Sortorder = Sortorder;
           var list= _db.Students.ToList();

            ViewBag.toatalpage = Math.Ceiling(list.Count()/10.0);
            list = list.Skip((PageNumber - 1) * 10).Take(10).ToList();
            switch (Sortby)
            {
                default:
                    break;
            }
            return View(list);
        }


Model(from database sql entity)

 public class Student
    {
        [Key]
        public int StudentId { get; set; }
        public string Name { get; set; }
        public int standard { get; set; }
        public string CollegeName { get; set; }
        public string Address { get; set; }

    }


View

@model IEnumerable<CodeFirstProject.Models.Student>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<script src="~/Scripts/bootstrap.min.js"></script>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <table class="table">
                <tr>
                    <th>
                        @Html.DisplayNameFor(model => model.Name)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.standard)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.CollegeName)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Address)
                    </th>

                </tr>

                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.standard)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.CollegeName)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Address)
                        </td>

                    </tr>


                }


            </table>
            <ul class="pagination">
                <li>
                    @{
                        double totalpage = ViewBag.toatalpage;

                        for (int i = 1; i <= totalpage; i++)
                        {

                            @Html.ActionLink(i.ToString(), "Index", new {Sortorder ="",Sortby="",PageNumber=i })

                        }

                    }
                </li>
            </ul>
        </div>



    </div>
</div>






No comments:

Post a Comment

Railway RRB Station Master Online Form 2026 Book

Railway RRB Station Master Online Form 2026 BOOK Book Link BUY NOW The Railway Recruitment Board (RRB) has officially issued the Centralise...