AjaxStoreResult 在 CustomerController 中的使用:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using CooliteMVC.Models; using CooliteMVC.Helper; using Coolite.Ext.Web; namespace CooliteMVC.Controllers { public class CustomerController : Controller { // // GET: /Customer/ public ActionResult Index() { ViewData["Title"] = "Customer List"; ViewData["Message"] = "Welcome to Coolite MVC! My name is Bruce."; return View(); } public ActionResult List(int limit, int start, string dir, string sort) { Random rand = new Random(); IList<Customer> list = new List<Customer>(); for (int i = start; i < start + limit; i++) list.Add(new Customer { CustomerID = "Customer" + i, Address = "Address" + i, City = "City" + rand.Next(1000), CompanyName = "Com" + rand.Next(1000), ContactName = "Contract" + rand.Next(1000), ContactTitle = "Title" + rand.Next(1000), Country = "Country" + rand.Next(1000), Email = rand.Next(1000) + "@live.com", Fax = rand.Next(1000).ToString() + rand.Next(1000), Mobile = rand.Next(1000).ToString() + rand.Next(1000), Notes = "Notes" + rand.Next(1000), Phone = "Phone" + rand.Next(1000), Region = "Region" + rand.Next(1000), TranDate = DateTime.Now.AddDays(rand.Next(30)) }); return new AjaxStoreResult(list, 100); } public ActionResult Save() { AjaxStoreResult ajaxStoreResult = new AjaxStoreResult(StoreResponseFormat.Save); try { StoreDataHandler dataHandler = new StoreDataHandler(Request["data"]); ChangeRecords<Customer> data = dataHandler.ObjectData<Customer>(); foreach (Customer customer in data.Deleted) { //db.Customers.Attach(customer); //db.Customers.DeleteOnSubmit(customer); } foreach (Customer customer in data.Updated) { //db.Customers.Attach(customer); //db.Refresh(RefreshMode.KeepCurrentValues, customer); } foreach (Customer customer in data.Created) { //db.Customers.InsertOnSubmit(customer); } } catch (Exception e) { ajaxStoreResult.SaveResponse.Success = false; ajaxStoreResult.SaveResponse.ErrorMessage = e.Message; } return ajaxStoreResult; } } }页面的关键代码:
<ext:Store ID="dsCustomers" runat="server" > <Proxy> <ext:HttpProxy Url="/Customer/List" Method ="GET" /> </Proxy> <UpdateProxy> <ext:HttpWriteProxy Url="/Customer/Save" /> </UpdateProxy> <Reader> <ext:JsonReader ReaderID="CustomerID" Root="data" TotalProperty="totalCount"> <Fields> <ext:RecordField Name="CustomerID" SortDir="ASC" /> <ext:RecordField Name="CompanyName" /> <ext:RecordField Name="ContactName" /> <ext:RecordField Name="Email" /> <ext:RecordField Name="Phone" /> <ext:RecordField Name="Fax" /> <ext:RecordField Name="Region" /> <ext:RecordField Name="TranDate" Type="Date" /> </Fields> </ext:JsonReader> </Reader> <BaseParams> <ext:Parameter Name="limit" Value="15" Mode="Raw" /> <ext:Parameter Name="start" Value="0" Mode="Raw" /> <ext:Parameter Name="dir" Value="ASC" /> <ext:Parameter Name="sort" Value="CustomerID" /> </BaseParams> <SortInfo Field="CustomerID" Direction="ASC" /> </ext:Store>我们可以看到其实就是Url的写法不同而已: <ext:HttpProxy Url="/Customer/List" Method ="GET" /> <ext:HttpWriteProxy Url="/Customer/Save" /> 详细页面代码跟第一章差不多,这里不列出来。