Spread for ASP.NET 10 Product Documentation > Developer's Guide > Managing Data Binding > Model Data Binding in ASP.NET 4.5 |
Spread for ASP.NET supports model data binding as provided by ASP.NET 4.5. The Spread component provides the ItemType property, as well as the SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod properties for working with model binding.
Model data binding only takes effect in .NET 4.5 or higher; if you try to use model data binding in another .NET environment, nothing happens. If you use model data binding and the DataSourceID property in the same project, the component throws an exception. If you use the DataSourceID and set one of the model data binding properties, such as SelectMethod or UpdateMethod, the component will also throw an exception.
The ItemType property indicates the type of data item used in model data binding. By default, it is empty. Set this property to use strongly typed data binding. If you set the ItemType property and set some sheets' SelectMethod property, the .NET framework will try to cast the data items to the type declared by the ItemType property. Therefore, you should set the ItemType and SelectMethod properties to the same data types, or set the SelectMethod to a parent data type. If you do not, the component will throw an exception.
The SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod properties set the name of the method to use to get, update, insert, or delete a data item in the data source. Note the following when using these properties.
This example code illustrates the model data binding properties.
The following code is added to the .aspx page:
<Sheets>
<FarPoint:SheetView SheetName="Sheet1"
AllowDelete="true" AllowInsert="true"
ItemType="DeptModel.User"
SelectMethod="GetUsers" UpdateMethod="UpdateUser" DeleteMethod="DeleteUser" InsertMethod="InsertUser">
</FarPoint:SheetView>
</Sheets>
Code is added to the .cs or .vb page to create the methods referred to in the .aspx page, as shown in the following sample.
C# |
Copy Code
|
---|---|
public IQueryable<User> GetUsers() { DeptEntities db = new DeptEntities(); return db.Users.AsQueryable(); } public bool UpdateUser(string login, string fullName, string email, string description) { int rowsAffected = -1; using (DeptEntities db = new DeptEntities()) { // user should exist in the database in order to be updated User found = db.Users.FirstOrDefault(u => u.Login == login); if (found == null) return false; // except login name, all other properties of a user can be changed found.FullName = fullName; found.Email = email; found.Description = description; if (ModelState.IsValid) { rowsAffected = db.SaveChanges(); } } // there should only be one user updated after running this update (1 row at a time) return rowsAffected == 1; } public bool InsertUser(string login, string fullName, string email, string description) { int rowsAffected = -1; using (DeptEntities db = new DeptEntities()) { // login name should be unique User found = db.Users.FirstOrDefault(u => u.Login == login); if (found != null) { string exceptionMessage = string.Format("Login name should be unique. There is an existing user with the login name of {0}", login); throw new InvalidOperationException(exceptionMessage); } // create new User var user = new User() { Login = login, FullName = fullName, Email = email, Description = description }; // add user to model, then commit changes if (ModelState.IsValid) { db.Users.AddObject(user); rowsAffected = db.SaveChanges(); } } return rowsAffected == 1; } public bool DeleteUser(string login) { int rowsAffected = -1; using (DeptEntities db = new DeptEntities()) { User found = db.Users.FirstOrDefault(u => u.Login == login); if (found != null) { db.Users.DeleteObject(found); rowsAffected = db.SaveChanges(); } } return rowsAffected == 1; } |
VB |
Copy Code
|
---|---|
Public Function GetUsers() As IQueryable(Of User) If found Is Nothing Then ' except login name, all other properties of a user can be changed If ModelState.IsValid Then ' there should only be one user updated after running this update (1 row at a time) Public Function InsertUser(login As String, fullName As String, email As String, description As String) As Boolean ' add user to model, then commit changes Return rowsAffected = 1 Public Function DeleteUser(login As String) As Boolean |