Skip to content

Instantly share code, notes, and snippets.

@rustd
Last active August 29, 2015 14:20
Show Gist options
  • Save rustd/f3ccd70472c9f06c3f47 to your computer and use it in GitHub Desktop.
Save rustd/f3ccd70472c9f06c3f47 to your computer and use it in GitHub Desktop.
aspnet46 modelbinding
<%--
Web Forms page calling a select method to model bind to a list of students.
The GridView uses Model Binding to bind to Student type in your app.
To use Async Model Binding, mark your page with Async=True attribute.
--%>
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" Async="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication226._Default" %>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView runat="server" ID="studentsGrid"
ItemType="ContosoUniversityModelBinding.Models.Student" DataKeyNames="StudentID"
SelectMethod="studentsGrid_GetData"
UpdateMethod="studentsGrid_UpdateItem"
DeleteMethod="studentsGrid_DeleteItem"
AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true"
AllowSorting="true" AllowPaging="true" PageSize="4"
AutoGenerateColumns="false">
<Columns>
<asp:DynamicField DataField="StudentID" />
<asp:DynamicField DataField="LastName" />
<asp:DynamicField DataField="FirstName" />
<asp:DynamicField DataField="Year" />
<asp:TemplateField HeaderText="Total Credits">
<ItemTemplate>
<asp:Label Text="<%# Item.Enrollments.Sum(en => en.Course.Credits) %>"
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
</asp:Content>
//Async method for Select
public async Task<SelectResult> studentsGrid_GetData(int startRowIndex, int maximumRows)
{
SchoolContext db = new SchoolContext();
var query = db.Students.Include(s => s.Enrollments.Select(e => e.Course));
SelectResult result = new SelectResult(query.Count(),
await query
.SortBy("StudentID")
.Skip(startRowIndex)
.Take(maximumRows)
.ToListAsync());
return result;
}
@IDisposable
Copy link

Line 4, Async=true is misspelled.

@rustd
Copy link
Author

rustd commented Jul 21, 2015

Fixed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment