asp.net web表单模型绑定

本主题说明如何使用模型绑定和Web表单处理数据。与处理诸如ObjectDataSource或SqlDataSource之类的数据源对象相比,模型绑定使数据交互更加直接。
在本教程中,我们将使用数据的实体框架和GridView在网页上显示数据。在这里,我们正在创建一个示例,其中包括以下步骤。

  1. 创建一个ASP.NET Web应用程序
  2. 选择模板
  3. 创建一个母版页
  4. 创建数据模型和数据库
// StudentModels.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data.Entity; using System.ComponentModel.DataAnnotations; namespace ModelBindingDemo.Models { public class SchoolContextDemo : DbContext { public DbSet< Student> Students { get; set; } public DbSet< Enrollment> Enrollments { get; set; } public DbSet< Course> Courses { get; set; } } public class Student { [Key, Display(Name = "Email ID")] [ScaffoldColumn(false)] public int StudentID { get; set; } [Required, StringLength(40), Display(Name = "Last Name")] public string LastName { get; set; } [Required, StringLength(20), Display(Name = "First Name")] public string FirstName { get; set; } [Required, StringLength(50), Display(Name = "Email ID")] public string Email { get; set; } [EnumDataType(typeof(AcademicYear)), Display(Name = "Academic Year")] public AcademicYear Year { get; set; } public virtual ICollection< Enrollment> Enrollments { get; set; } } public class Enrollment { [Key] public int EnrollmentID { get; set; } public int CourseID { get; set; } public int StudentID { get; set; } public decimal? Grade { get; set; } public virtualCourse Course { get; set; } public virtualStudent Student { get; set; } } public class Course { [Key] public int CourseID { get; set; } public string Title { get; set; } public int Credits { get; set; } public virtual ICollection< Enrollment> Enrollments { get; set; } } public enum AcademicYear { Freshman, Sophomore, Junior, Senior } }

SchoolContextDemo类派生自DbContext,后者管理数据库连接和数据更改。
我们将使用代码优先迁移工具基于这些类建立数据库。遵循视图-> 其他窗口-> Package Manager控制台,从菜单栏中打开Package Manager控制台。
它将提示在Visual Studio IDE底部的屏幕。我们需要在此控制台中执行以下命令。
enable-migrations -Force -ContextTypeName ModelBindingDemo.Models.SchoolContextDemo

执行上述命令后,将产生以下输出。
asp.net web表单模型绑定

文章图片
之后,创建了一个名为Configuration.cs的新文件。该文件在创建后自动打开。该类包含一个Seed方法,使我们能够使用测试数据预先填充数据库表。
在这里,我们将一些数据添加到配置文件中,以便在网页上向用户显示。将数据添加到文件后,我们的Configuration.cs文件如下所示:
// Configuration.cs
namespace ModelBindingDemo.Migrations { using System; using System.Data.Entity; using System.Data.Entity.Migrations; using System.Linq; using ModelBindingDemo.Models; internal sealed class Configuration : DbMigrationsConfiguration< ModelBindingDemo.Models.SchoolContextDemo> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(ModelBindingDemo.Models.SchoolContextDemo context) { context.Students.AddOrUpdate( new Student { FirstName = "M. Irfan", LastName = "Khan", Email = "irfan@example.com", Year = AcademicYear.Freshman }, new Student { FirstName = "Arvind", LastName = "Kumar", Email = "Arvind@example.com", Year = AcademicYear.Freshman }, new Student { FirstName = "Arturo", LastName = "Anand", Email = "Anand@example.com", Year = AcademicYear.Sophomore }, new Student { FirstName = "Moris", LastName = "Mano", Email = "moris@example.com", Year = AcademicYear.Sophomore }, new Student { FirstName = "Roman", LastName = "Sigh", Email = "roman@example.com", Year = AcademicYear.Junior }, new Student { FirstName = "Jimmi", LastName = "Seth", Email = "jimmi@example.com", Year = AcademicYear.Junior }, new Student { FirstName = "Shayam", LastName = "Rana", Email = "Shayam@example.com", Year = AcademicYear.Senior }, new Student { FirstName = "Jecub", LastName = "Cunto", Email = "Nino@example.com", Year = AcademicYear.Senior } ); context.SaveChanges(); context.Courses.AddOrUpdate( new Course { Title = "Chemistry", Credits = 3 }, new Course { Title = "Microeconomics", Credits = 3 }, new Course { Title = "Macroeconomics", Credits = 3 }, new Course { Title = "Calculus", Credits = 4 }, new Course { Title = "Trigonometry", Credits = 4 }, new Course { Title = "Composition", Credits = 3 }, new Course { Title = "Literature", Credits = 4 } ); context.SaveChanges(); context.Enrollments.AddOrUpdate( new Enrollment { StudentID = 1, CourseID = 1, Grade = 1 }, new Enrollment { StudentID = 1, CourseID = 2, Grade = 3 }, new Enrollment { StudentID = 1, CourseID = 3, Grade = 1 }, new Enrollment { StudentID = 2, CourseID = 4, Grade = 2 }, new Enrollment { StudentID = 2, CourseID = 5, Grade = 4 }, new Enrollment { StudentID = 2, CourseID = 6, Grade = 4 }, new Enrollment { StudentID = 3, CourseID = 1 }, new Enrollment { StudentID = 4, CourseID = 1 }, new Enrollment { StudentID = 4, CourseID = 2, Grade = 4 }, new Enrollment { StudentID = 5, CourseID = 3, Grade = 3 }, new Enrollment { StudentID = 6, CourseID = 4 }, new Enrollment { StudentID = 7, CourseID = 5, Grade = 2 } ); context.SaveChanges(); } } }

之后,在程序包管理器控制台中,运行以下命令。
PM> add-migration initial


PM> update-database

asp.net web表单模型绑定

文章图片
现在,已创建一个数据库并将其添加到App_Data文件夹下的项目中。
现在,让我们将数据从创建的数据库显示到网页上。我们将使用GridView控件以网格形式显示数据。 Student.aspx文件如下所示:
// Student.aspx
< %@PageTitle="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Students.aspx.cs" Inherits="ModelBindingDemo.Students" %> < asp:ContentID="Content1"ContentPlaceHolderID="MainContent"runat="server"> < asp:GridViewrunat="server"ID="studentsGrid" ItemType="ModelBindingDemo.Models.Student"DataKeyNames="StudentID" SelectMethod="studentsGrid_GetData" AutoGenerateColumns="false"> < Columns> < asp:DynamicFieldDataField="StudentID"/> < asp:DynamicFieldDataField="FirstName"/> < asp:DynamicFieldDataField="LastName"/> < asp:DynamicFieldDataField="Email"/> < /Columns> < /asp:GridView> < /asp:Content>

背后的代码
// Student.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using ModelBindingDemo.Models; using System.Data.Entity; namespace ModelBindingDemo { public partial class Students : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public IQueryable< Student> studentsGrid_GetData() { SchoolContextDemo db = newSchoolContextDemo(); var query = db.Students.Include(s => s.Enrollments.Select(e => e.Course)); return query; } } }

最后,我们的项目如下所示。
asp.net web表单模型绑定

文章图片
输出:
【asp.net web表单模型绑定】在浏览器中运行Student.aspx文件作为视图,它将产生以下输出。
asp.net web表单模型绑定

文章图片

    推荐阅读