• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

asp 搞代码 4年前 (2022-01-03) 22次浏览 已收录 0个评论

啊,开始以为MVC+Coolite结合的例子没什么难度,但原来Coolite在MVC中需要特定设置一下某些属性才行,费了两个小时才算大功告成,具体请看下文。还是先把这个例子的效果贴上来再说。

因为默认的 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的一般用法:

<ext:jsonreader readerid="CustomerID" root="data” totalproperty=”totalCount“> 

所以, 要继承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 { publicclass AjaxStoreResult : ActionResult { public AjaxStoreResult() { } public AjaxStoreResult(object data) { this.Data = data; } public 来源gao@!dai!ma.com搞$$代^@码网AjaxStoreResult(object data, int totalCount) : this(data) { this.TotalCount = totalCount; } public AjaxStoreResult(StoreResponseFormat responseFormat) { this.ResponseFormat = responseFormat; } privateobject data; publicobject Data { get { returnthis.data; } set { this.data = value; } } privateint totalCount; publicint TotalCount { get { returnthis.totalCount; } set { this.totalCount = value; } } private StoreResponseFormat responseFormat = StoreResponseFormat.Load; public StoreResponseFormat ResponseFormat { get { returnthis.responseFormat; } set { this.responseFormat = value; } } private SaveStoreResponse saveResponse; public SaveStoreResponse SaveResponse { get { if (this.saveResponse == null) { this.saveResponse = new SaveStoreResponse(); } returnthis.saveResponse; } } publicoverridevoid 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: thrownew ArgumentOutOfRangeException(); } } } publicenum StoreResponseFormat { Load, Save } publicclass SaveStoreResponse { privatebool success = true; privatestring errorMessage; publicbool Success { get { returnthis.success; } set { this.success = value; } } publicstring ErrorMessage { get { returnthis.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;
/*white-space: pre;*/
}
.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 { publicclass 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)) }); returnnew 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;
/*white-space: pre;*/
}
.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; }

页面的关键代码:

   <ext:store id="dsCustomers" runat="server" >  <ext:httpproxy url="/Customer/List" Method ="GET" />  <ext:httpwriteproxy url="/Customer/Save" />  <ext:jsonreader readerid="CustomerID" Root="data" TotalProperty="totalCount">  <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" />  <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" /> <sortinfo field="CustomerID" Direction="ASC" /> 
我们可以看到其实就是Url的写法不同而已:
 <ext:httpproxy url="/Customer/List" Method ="GET" />
 <ext:httpwriteproxy url="/Customer/Save" /> 
详细页面代码跟第一章差不多,这里不列出来。 

以上就是Coolite Cool Study 3 MVC + Coolite 的实现代码的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Coolite Cool Study 3 MVC + Coolite 的实现代码

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址