Last active
August 29, 2015 14:20
-
-
Save rustd/f3ccd70472c9f06c3f47 to your computer and use it in GitHub Desktop.
aspnet46 modelbinding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%-- | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed. Thanks!