欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Coolite Cool Study 3 MVC + Coolite 的實現(xiàn)代碼

 更新時間:2009年05月17日 09:43:24   作者:  
啊,開始以為MVC+Coolite結(jié)合的例子沒什么難度,但原來Coolite在MVC中需要特定設(shè)置一下某些屬性才行,費了兩個小時才算大功告成,具體請看下文。還是先把這個例子的效果貼上來再說。
MVC-Coolite

因為默認的 MVC 的樣式文件里對于的 table 和 其他相關(guān)樣式(h1~h6) 與Coolite有沖突,會導(dǎo)致GridPanel走樣,大家記得先把那個table 和  h1~h6的樣式清除掉才看到GridPanel的帥臉面 …

項目文件分布:

ProjectFiles

關(guān)于Coolite在MVC中的配置文件跟一般webform是一樣的。 但在MVC的Global.asax中,需要在 RegisterRoutes 方法里加上這一句:

routes.IgnoreRoute("{exclude}/{coolite}/coolite.axd");

另外 ScriptManager 要注明 IDMode="Static“:

<ext:ScriptManager ID="ScriptManager1" runat="server"  IDMode="Static"/>

其中唯一與一般MVC不同的是,我們需要定義自己的ActionResult來返回Json結(jié)果給客戶端。因為Coolite 的JsonReader 要求的格式大致都是這樣:{data: [{…}], totalCount: …}

關(guān)于JsonReader的一般用法:

<ext:JsonReader ReaderID="CustomerID" Root="data" TotalProperty="totalCount"> 

所以, 要繼承MVC ActionResult 的抽象方法 public override void ExecuteResult(ControllerContext context)  來返回給 JsonReader   合適口味的 JsonResult , 不然它就不認人了。

以下代碼實現(xiàn)了對Json Response & Save Response 的簡單封裝。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Coolite.Ext.Web;

namespace CooliteMVC.Helper
{ 
  public class AjaxStoreResult : ActionResult
  {
    public AjaxStoreResult() { }

    public AjaxStoreResult(object data)
    {
      this.Data = data;
    }

    public AjaxStoreResult(object data, int totalCount)
      : this(data)
    {
      this.TotalCount = totalCount;
    }

    public AjaxStoreResult(StoreResponseFormat responseFormat)
    {
      this.ResponseFormat = responseFormat;
    }

    private object data;
    public object Data
    {
      get { return this.data; }
      set { this.data = value; }
    }

    private int totalCount;
    public int TotalCount
    {
      get { return this.totalCount; }
      set { this.totalCount = value; }
    }

    private StoreResponseFormat responseFormat = StoreResponseFormat.Load;
    public StoreResponseFormat ResponseFormat
    {
      get { return this.responseFormat; }
      set { this.responseFormat = value; }
    }

    private SaveStoreResponse saveResponse;
    public SaveStoreResponse SaveResponse
    {
      get
      {
        if (this.saveResponse == null)
        {
          this.saveResponse = new SaveStoreResponse();
        }
        return this.saveResponse;
      }
    }

    public override void ExecuteResult(ControllerContext context)
    {
      switch (this.ResponseFormat)
      {
        case StoreResponseFormat.Load:

          string json = Coolite.Ext.Web.JSON.Serialize(Data);
          json = "{data:" + json + ", totalCount:" + 100 + "}";
          context.HttpContext.Response.Write(json);
           
          break;
        case StoreResponseFormat.Save:
          Response response = new Response(true);
          response.Success = this.SaveResponse.Success;
          response.Msg = this.SaveResponse.ErrorMessage;
          StoreResponseData saveResponse = new StoreResponseData();
          saveResponse.Confirmation = this.SaveResponse.ConfirmationList;
          response.Data = saveResponse.ToString();

          response.Write();
          break;
        default:
          throw new ArgumentOutOfRangeException();
      }
    }
 
  }

  public enum StoreResponseFormat
  {
    Load,
    Save
  }

  public class SaveStoreResponse
  {
    private bool success = true;
    private string errorMessage;

    public bool Success
    {
      get { return this.success; }
      set { this.success = value; }
    }

    public string ErrorMessage
    {
      get { return this.errorMessage; }
      set { this.errorMessage = value; }
    }

    public ConfirmationList ConfirmationList { get; set; }
  }
}

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;
    }
 
  }
}

頁面的關(guān)鍵代碼:

   <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" /> 
詳細頁面代碼跟第一章差不多,這里不列出來。 

相關(guān)文章

  • asp.net Web.config 詳細配置說明

    asp.net Web.config 詳細配置說明

    asp.net開發(fā)的朋友,經(jīng)常用得到web.config文件的配置,所以我們特整理了中文說明。
    2009-06-06
  • .NET Core中的HttpClientFactory類用法詳解

    .NET Core中的HttpClientFactory類用法詳解

    本文詳細講解了.NET Core中的HttpClientFactory類的用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • WPF中自定義GridLengthAnimation

    WPF中自定義GridLengthAnimation

    這篇文章主要為大家詳細介紹了WPF中自定義GridLengthAnimation的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • ASP.NET主機資源控制的一些心得

    ASP.NET主機資源控制的一些心得

    您可以通過以下設(shè)置控制ASP.NET主機對服務(wù)器內(nèi)存的占用。并能設(shè)置ASP.NET主機進程定時重建這樣可以避免服務(wù)器長時間運行aspnet占用大量空閑內(nèi)存,有利于提高aspnet運行效率。
    2013-02-02
  • Entity Framework使用Code First模式管理數(shù)據(jù)庫

    Entity Framework使用Code First模式管理數(shù)據(jù)庫

    本文詳細講解了Entity Framework使用Code First模式管理數(shù)據(jù)庫的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • MVC、MVP和MVVM分別是什么_動力節(jié)點Java學(xué)院整理

    MVC、MVP和MVVM分別是什么_動力節(jié)點Java學(xué)院整理

    MVC,MVP 和 MVVM分別是什么?MVC(Model-View-Controller)是最常見的軟件架構(gòu)之一,業(yè)界有著廣泛應(yīng)用。它本身很容易理解,但是要講清楚,它與衍生的 MVP 和 MVVM 架構(gòu)的區(qū)別就不容易了。
    2017-08-08
  • C#/VB.NET 在Word中添加條碼、二維碼的示例代碼

    C#/VB.NET 在Word中添加條碼、二維碼的示例代碼

    這篇文章主要介紹了C#/VB.NET 如何在Word中添加條碼、二維碼,代碼中將分為在Word正文段落中、頁眉頁腳中等情況來添加。感興趣的朋友可以了解下
    2020-07-07
  • 服務(wù)器讀取EXCEL不安裝OFFICE如何實現(xiàn)

    服務(wù)器讀取EXCEL不安裝OFFICE如何實現(xiàn)

    用asp.net做了一簡單的游戲管理后臺,涉及到了上傳Excel導(dǎo)入數(shù)據(jù)的功能,在本地開發(fā)實現(xiàn)都好好的,可已上傳的服務(wù)器上就悲劇了,下面有個不錯的解決方法,大家可以參考下
    2014-03-03
  • .NET下實現(xiàn)數(shù)字和字符相混合的驗證碼實例

    .NET下實現(xiàn)數(shù)字和字符相混合的驗證碼實例

    這篇文章介紹了.NET下實現(xiàn)數(shù)字和字符相混合的驗證碼實例,有需要的朋友可以參考一下
    2013-11-11
  • asp.net Grid 導(dǎo)出Excel實現(xiàn)程序代碼

    asp.net Grid 導(dǎo)出Excel實現(xiàn)程序代碼

    看了FineUI中的將Grid導(dǎo)出為Excel的實現(xiàn)方法,實際上是可以非常簡單??磥砗茈y的問題,變換一種思路就可以非常簡單
    2012-12-12

最新評論