How to display 20 records each page in mvc4?

Posted by Mandlaa under ASP.NET MVC on 1/8/2014 | Points: 10 | Views : 9142 | Status : [Member] | Replies : 13
How can i display for each page 20 records Just like asp.net gridview Pagesize="20" property,

In the same way i want to display 20 records for each page in MVC4?

How can i do this?

@model IEnumerable<Hoda.Models.Products>

@{
ViewBag.Title = "products";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<p>

@Html.ActionLink("Create", "Create")

{

</p>
<table class="table table-striped" style="width:auto">


<tr>
<th>
@Html.DisplayNameFor(model => model.No)
</th>

<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.Specifications)
</th>
<th>
@Html.DisplayNameFor(model => model.Quality_ValueAnalysis)
</th>
<th>
@Html.DisplayNameFor(model => model.Aboutus)
</th>

<th>
@Html.DisplayNameFor(model => model.Price)
</th>



</tr>
@if (Model.Count() == 0)
{
<tr>
<td colspan="4">No rows match for this search</td>
</tr>
}
else
{

foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.No)
</td>

<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>

<td>
@Html.Raw(item.Specifications)
</td>
<td>
@Html.Raw(item.Quality_ValueAnalysis)
</td>
<td>
@Html.Raw(item.Aboutus)
</td>
@*<td>
@Html.Raw(item.Reviews)
</td>*@
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>



<td>
@Html.ActionLink("Edit", "Edit", new { id = item.TagNo })
@Html.ActionLink("Details", "Newcars", new { id = item.TagNo})

</td>


</tr>
}
}

</table>
Present i am using to display all records above code,

cs

public ActionResult Index()
{
var styles = from s in db.demoproducts select s;
return View(styles);
}




Responses

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25
Posted by: Mandlaa on: 1/8/2014 [Member] Starter | Points: 25

Up
0
Down
VIEW:
@using PagedList
@using PagedList.Mvc
@model IEnumerable<Hoda.Models.demoproduct>

@{
ViewBag.Title = "Neww products";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<p>
<a href="~/Addnew/Create" class="btn btn-primary" style="text-align:center">Add New Car</a>

@using (Html.BeginForm("Index", "Addnew", FormMethod.Get))
{

<b>Search by</b>
@Html.RadioButton("searchBy", "TagNo", true)<text>Tagno</text>
@Html.RadioButton("searchBy", "StockNo", true)<text>StockNo</text>
<br />
@Html.TextBox("search")<input type="submit" value="Search" />
}

</p>
<table class="table table-striped" style="width:auto">


<tr>
<th>
@Html.DisplayNameFor(model => model.TagNo)
</th>
<th>
@Html.DisplayNameFor(model => model.StockNo)
</th>

<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.Specifications)
</th>
<th>
@Html.DisplayNameFor(model => model.Quality_ValueAnalysis)
</th>
<th>
@Html.DisplayNameFor(model => model.Aboutus)
</th>

<th>
@Html.DisplayNameFor(model => model.Price)
</th>

</tr>
@if (Model.Count() == 0)
{
<tr>
<td colspan="4">No rows match for this search</td>
</tr>
}
else
{

foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TagNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.StockNo)
</td>

<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>

<td>
@Html.Raw(item.Specifications)
</td>
<td>
@Html.Raw(item.Quality_ValueAnalysis)
</td>
<td>
@Html.Raw(item.Aboutus)
</td>

<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<td>


<td>
@Html.ActionLink("Edit", "Edit", new { id = item.TagNo })
@Html.ActionLink("Details", "Newcars", new { id = item.TagNo })


</td>


</tr>
}
}

</table>

public ActionResult Index(string searchBy, string search)
{

if (searchBy == "TagNo")
{
return View(db.demoproducts.Where(x => x.TagNo == search || search == null).ToList());
}
else
{
return View(db.demoproducts.Where(x => x.StockNo.StartsWith(search) || search == null).ToList());
}
}

This is my actual code,
I want paging How can i do?
Plz change my above code

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
pagesize and pageNumber Variables ate global.
int pageSize;
int pageNumber;

public ActionResult Index(string searchBy, string search,int? page )
{
pageSize =20;
pageNumber = (page ?? 1);


if (searchBy == "TagNo")
{
return View(db.demoproducts.Where(x => x.TagNo == search || search == null).ToList().ToPagedList(pageNumber, pageSize) );
}
else
{
return View(db.demoproducts.Where(x => x.StockNo.StartsWith(search) || search == null).ToList().ToPagedList(pageNumber, pageSize) );
}
}


Thank you,
Govind

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Sorry forget to post, view Code

In View: Add Below code
 <tr>
<td colspan="3">
@Html.PagedListPager(Model, page => Url.Action("Home", new { page }))
</td>
<td colspan="1" nowrap>
<div id='Paging' style="float: left;">
<b>Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount &nbsp;|&nbsp; Total Records: @TempData["NoOfRowscount"]</b>
</div>
</td>
</tr>


Thank you,
Govind

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/8/2014 [Member] Starter | Points: 25

Up
0
Down
@using PagedList

@using PagedList.Mvc
@model IEnumerable<Hoda.Models.demoproduct>

@{
ViewBag.Title = "Neww products";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<p>
<a href="~/Addnew/Create" class="btn btn-primary" style="text-align:center">Add New Car</a>

@using (Html.BeginForm("Index", "Addnew", FormMethod.Get))
{

<b>Search by</b>
@Html.RadioButton("searchBy", "TagNo", true)<text>Tagno</text>
@Html.RadioButton("searchBy", "StockNo", true)<text>StockNo</text>
<br />
@Html.TextBox("search")<input type="submit" value="Search" />
}

</p>
<table class="table table-striped" style="width:auto">


<tr>
<th>
@Html.DisplayNameFor(model => model.TagNo)
</th>
<th>
@Html.DisplayNameFor(model => model.StockNo)
</th>

<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.Specifications)
</th>
<th>
@Html.DisplayNameFor(model => model.Quality_ValueAnalysis)
</th>
<th>
@Html.DisplayNameFor(model => model.Aboutus)
</th>

<th>
@Html.DisplayNameFor(model => model.Price)
</th>

</tr>
@if (Model.Count() == 0)
{
<tr>
<td colspan="4">No rows match for this search</td>
</tr>
}
else
{

foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TagNo)
</td>
<td>
@Html.DisplayFor(modelItem => item.StockNo)
</td>
@*<td>
@Html.DisplayFor(modelItem => item.BatchId)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClientId)
</td>*@
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>

<td>
@Html.Raw(item.Specifications)
</td>
<td>
@Html.Raw(item.Quality_ValueAnalysis)
</td>
<td>
@Html.Raw(item.Aboutus)
</td>
@*<td>
@Html.Raw(item.Reviews)
</td>*@
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>

<td>
@Html.ActionLink("Edit", "Edit", new { id = item.TagNo })
@Html.ActionLink("Details", "Newcars", new { id = item.TagNo })

</td>
<tr>
<td colspan="3">
@Html.PagedListPager(Model, page => Url.Action("Home", new { page }))
</td>
<td colspan="1" nowrap>
<div id="Paging" style="float: left;">
<b>
Page @(Model.PageCount <Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount &nbsp;|&nbsp; Total Records: @TempData["NoOfRowscount"]
</b>
</div>
</td>
</tr>

</tr>
}
}

</table>

I am writting this getting an error
ERROR:
ompilation Error



Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Check you using statements.

Replace with these statements

@model PagedList.IPagedList<Hoda.Models.demoproduct>
@using PagedList.Mvc;


And then Build the solution

Thank you,
Govind

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/8/2014 [Member] Starter | Points: 25

Up
0
Down
Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'PagedList.IPagedList<Hoda.Models.demoproduct>' does not contain a definition for 'TagNo' and no extension method 'TagNo' accepting a first argument of type 'PagedList.IPagedList<Hoda.Models.demoproduct>' could be found (are you missing a using directive or an assembly reference?)

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

This is not paging Issue.
You have to Check your model Class 'demoproduct' and check there is a property 'TagNo'


Thank you,
Govind

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,

Revert back, if you have any issues
if not Please Mark as answer.

Thank you,
Govind

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/8/2014 [Member] Starter | Points: 25

Up
0
Down
TagNo property is their in my model

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/9/2014 [Member] [MVP] Bronze | Points: 25

Up
0
Down
Hi,
Send me Your model class, controller, and view.
i will go through it. attach here.

Thank you,
Govind

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chakravarthi on: 1/9/2014 [Member] Starter | Points: 25

Up
0
Down
Use Webgrid helper class to show records in gridview like asp.net gridview

http://www.dotnetfunda.com/articles/show/2727/using-webgrid-to-populate-data-as-gridview-in-aspnet-mvc-razorview


Chakravarthi

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/9/2014 [Member] Starter | Points: 25

Up
0
Down
This is my model
SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[demoproducts](
[ID] [int] IDENTITY(1,1) NOT NULL,
[TagNo] [varchar](max) NULL,
[StockNo] [varchar](max) NULL,
[BatchId] [int] NULL,
[ClientId] [int] NULL,
[VinNo] [nvarchar](max) NULL,
[ProductName] [nvarchar](max) NULL,
[Specifications] [nvarchar](max) NULL,
[Quality&ValueAnalysis] [nvarchar](max) NULL,
[Aboutus] [nvarchar](max) NULL,
[Reviews] [nvarchar](max) NULL,
[Price] [nvarchar](max) NULL,

[Sno] [varchar](max) NULL,
CONSTRAINT [PrimaryKey_01827ad6-c73c-4cf2-a317-4659f2667c72] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO

SET ANSI_PADDING OFF
GO

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response