栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

Coolite Cool Study 3 MVC + Coolite 的实现代码

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Coolite Cool Study 3 MVC + Coolite 的实现代码


因为默认的 MVC 的样式文件里对于的 table 和 其他相关样式(h1~h6) 与Coolite有冲突,会导致GridPanel走样,大家记得先把那个table 和  h1~h6的样式清除掉才看到GridPanel的帅脸面 …

项目文件分布:

关于Coolite在MVC中的配置文件跟一般webform是一样的。 但在MVC的Global.asax中,需要在 RegisterRoutes 方法里加上这一句:

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

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

其中唯一与一般MVC不同的是,我们需要定义自己的ActionResult来返回Json结果给客户端。因为Coolite 的JsonReader 要求的格式大致都是这样:{data: [{…}], totalCount: …}

关于JsonReader的一般用法:

 

所以, 要继承MVC ActionResult 的抽象方法 public override void ExecuteResult(ControllerContext context)  来返回给 JsonReader   合适口味的 JsonResult , 不然它就不认人了。

以下代码实现了对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./confirm/iationList;
   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; }
  }
}
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

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 list = new List();
      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 data = dataHandler.ObjectData();

 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;
    }
 
  }
}
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

页面的关键代码:

   
    
      
    
    

    
    
      
 
   
   
   
   
   
   
   
   
 
      
    
    
      
      
      
      
    
    
  
我们可以看到其实就是Url的写法不同而已:
 
  
详细页面代码跟第一章差不多,这里不列出来。 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/59306.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号