Tuesday, 20 October 2015

ASP.NET MVC WebGrid Helper

ASP.NET MVC - Get the data from database and display as sort-able grid:

.cshtml page
@{
    var db = Database.Open("tutorial");   // db connection string
    var selectQueryString = "SELECT * FROM students ORDER BY id";   // sort by id by default
    var data = db.Query(selectQueryString);
    var grid = new WebGrid(data);
}

<div id="grid">
    @grid.GetHtml()
</div>


Web config database connection string:
<add name="tutorial" connectionString="Data Source=localhost;Initial Catalog=students;Integrated Security=true;" providerName="System.Data.SqlClient"/>


No comments:

SQL: Generate a range of numbers

SELECT ones.n + 10*tens.n + 100*hundreds.n + 1000*thousands.n FROM       (VALUES(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) ones(n),      (VALU...