Paging in MVC 5 - asp.net-mvc

In MVC 5, how to create paging for table? The following is a table structure.
<table class="table">
<tr>
<th>
Item ID
</th>
<th>
Item Name
</th>
<th>
Rate
</th>
<th>
Stock
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.ItemID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ItemName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stock)
</td>
</tr>
}
</table>

I recommend the PagedList package which is available on nuget
https://www.nuget.org/packages/PagedList
Here is the full documentation
https://github.com/TroyGoode/PagedList

This tutorial has a good explanation of paging and how to install and use the packages:
http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

I have found the solution. Using webgrid, we can achieve the paging
#model IEnumerable<ItemMaster>
#{
ViewBag.Title = "Item Details";
WebGrid grid = new WebGrid(Model,rowsPerPage:5);
}
<h2>Item Details</h2>
#grid.GetHtml()

Grid.MVC is an open source and working Grid with sorting and paging
(and unlike telerik you do not need to change anything on controller side just add reference in your view and use.)
To install nugget package (Install)
PM> Install-Package Grid.Mvc -Version 3.0.0
In your view you can auto-create columns as below (or customize see User Guide)
#using GridMvc.Html
#Html.Grid(Model).AutoGenerateColumns()
See Online Demo

you can look for DataTables jquery plugin here. It is a very powerful and light weight plugin that supports sorting and searching inside the table.

Related

How can I do ASP.NET MVC view scaffolding in Rider IDE?

Currently trying Rider (JetBrains IDE for .Net). I used to work on Visual Studio Enterprise for c# asp.net MVC projects, and i'd like to know if there's a way (on Rider) to do like the "Add -> view -> with create/delete/update/list" feature on Visual Studio?
Something which would generate CRUD views like that :
#model IEnumerable<Web.Models.Warrior>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Health)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Health)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
PS : if there's a way to do it with controllers to... :D
But you still can...
You can use dotnet aspnet-codegenerator terminal tool. which is used for generating controllers and views with different options.
Install the tool globally.
dotnet tool install --global dotnet-aspnet-codegenerator
dotnet aspnet-codegenerator controller -name MyNewController -m MyModel -dc MyDbContext -outDir Controllers/
You can vote\track this issue: https://youtrack.jetbrains.com/issue/RIDER-12363
This is called Scaffolding and is unfortunately not a feature in Rider. I couldn't tell you if it's a planned feature, but I hope so.
My only suggestion might be to open VS, scaffold, and then use Rider again to continue coding.

MVC Turn model text into clickable link

I have an MVC Razor view that pulls change log information from a database (a table which only I have access to, limiting security concerns).
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
</tr>
}
</table>
The description will contain values like New page available: http://localhost:0000/app/NewItem. What I'm trying to figure out is how to make that web address active.
I.e., turn New page available: http://localhost:0000/app/NewItem
into: New page available: http://localhost:0000/app/NewItem
and have the link properly rendered on the page. Is there a simple way to accomplish this, short of using regular expressions in the controller to pull that link out?

How do I write VB Inline html templates in Razor?

I am struggling with writing in-line templates in razor vb.
Here is a sample forms c# version. Can someone translate to RAZOR vb?
<%Html.GridView<Employee>(
this.ViewData.Model,
data => { %>
<table class="grid" cellpadding="0" cellspacing="0">
<tr>
<th> </th>
<th> </td>
<th> </td>
<th>Name</th>
<th>E-mail</th>
</tr>
<% },
It does not appear, even in the latest Visual Studio, that this was supported, which is very unfortunate. It does seem templates are supported to some degree (as taken from this), but not for complex control setups like you have above. Bummer.

MVC 3 Razor view renders different on hosting environment than locally

I have created an MVC 3 view, just by adding a controller and scaffold it by usage of entity framework.
See below view:
<table>
<tr>
<th>
MetaType
</th>
<th>
Value
</th>
<th>
Page
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.MetaType)
</td>
<td>
#Html.DisplayFor(modelItem => item.Value)
</td>
<td>
#Html.DisplayFor(modelItem => item.Page.Title)
</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>
When I open the view on my dev machine than the html produced is just as expected:
<table>
<tr>
<th>
MetaType
</th>
<th>
Value
</th>
<th>
Page
</th>
<th></th>
</tr>
<tr>
<td>
Keywords
</td>
<td>
bla, bla bla,
</td>
<td>
Home
</td>
<td>
Edit |
Details |
Delete
</td>
</tr>
On our hosting environment it's rendered with on almost every row an extra whiteline.
<table>
<tr>
<th>MetaType</th>
<th>Value</th>
<th>Page</th>
<th></th>
</tr>
<tr>
<td>Keywords</td>
<td>values, values, values.</td>
<td>Home</td>
<td>Edit | Delete</td>
</tr>
Anyone has an idea why there is a difference, and how to resolve this extra whitespace?
UPDATE
It seems like only this page is having this issue. Other pages just have a normal html layout as expected. I also have checked the data from the database, but can't find anything unusual or different than the other pages.
I have also tried to regenerate the file and upload it again, but also no luck. I'm a bit out of options on this issue.
When something different in a host comparing to your environment, it's usually one of the following issues:
Time and/or DateTime Format of server is different from your local machine
Global application culture and/or encoding is different
Databases (MS-SQL) has a different collisions
I suggest you to add the following node to both your local and host web.config, and check the result:
<configuration>
<system.web>
<globalization
requestEncoding="utf-8"
responseEncoding="utf-8" />
</system.web>
</configuration>
After a doing a republish of my application the issue has dissapeared. Haven't found what's causing it.

How Add fake row in Grid helper Telerik to ASP.NET MVC

I need to add a line in the thead of table that going contenter inputs and selects that are used as parameters for filtering.
It is more or less how I wanted to stay:
<table>
<thead>
<tr>
<th> Name </ th> ...
<th> Type </ th>
</tr>
<tr> <!-- this is the 'tr fake' in thead -->
<th> <input type="text"/> </ th>
<th> <select name="nb"> ...</ select> </ th>
<tr>
</thead>
<tbody> .....
</table>
How do I add the this tr in table using the helper telerik grid for ASP.NET MVC?
Telerik ASP.NET MVC 2010 Q3 Beta has Custom Toolbar template functionality implemented. An example can be seen in demo which actually does implement pretty much what you want.

Resources