ASP.NET Web Pages - assistente WebGrid

WebGrid - uno degli utili assistenti ASP.NET Web.

Scrivere HTML manualmente

Nelle sezioni precedenti, abbiamo utilizzato il codice razor per visualizzare i dati del database e abbiamo scritto manualmente tutti i tag HTML:

Esempio di istanza Database

@{
var db = Database.Open("SmallBakery"); 
var selectQueryString = "SELECT * FROM Product ORDER BY Name"; 
}
<html> 
<body> 
<h1>Prodotti piccola pasticceria</h1> 
<table> 
<tr>
<th>Id</th> 
<th>Prodotto</th> 
<th>Descrizione</th> 
<th>Prezzo</th> 
</tr>
@foreach(var row in db.Query(selectQueryString))
{
<tr> 
<td>@row.Id</td> 
<td>@row.Name</td> 
<td>@row.Description</td> 
<td align="right">@row.Price</td> 
</tr> 
}
</table> 
</body> 
</html>

Esegui esempio

Uso del WebGrid Helper

L'uso del WebGrid Helper è un metodo più semplice per visualizzare i dati.

WebGrid Helper:

  • Crea automaticamente la tabella HTML per visualizzare i dati
  • Supporta diverse opzioni di formattazione
  • Supporta la paginazione dei dati
  • Supporta l'ordinamento tramite il titolo della colonna

Esempio WebGrid

@{ 
var db = Database.Open("SmallBakery") ; 
var selectQueryString = "SELECT * FROM Product ORDER BY Id"; 
var data = db.Query(selectQueryString); 
var grid = new WebGrid(data); 
}
<html> 
<head> 
<title>Visualizzazione dei dati utilizzando il WebGrid Helper</title> 
</head> 
<body> 
<h1>Prodotti piccola pasticceria</h1> 
<div id="grid"> 
@grid.GetHtml()
</div> 
</body> 
</html>

Esegui esempio