当前位置:   article > 正文

ASP.NET MVC 快速开发框架之 SqlSugar+SyntacticSugar+JQWidgetsSugar+jqwidgets

asp.net mvc sqlsugar mysql

jqwidgets.js:

是一个功能完整的框架,它具有专业的可触摸的jQuery插件、主题、输入验证、拖放插件、数据适配器,内置WAI-ARIA(无障碍网页应用)可访问性、国际化和MVVM模式支持。jQWidgets 为搭建专业网站和开发移动应用程序提供了一个全面的解决方案。它完全基于开放的标准和技术,如 HTML5、CSS、Javascript和jQuery。jQWidgets能实现响应式web开发,可以帮助您创建在桌面、平板电脑和智能手机上看起来很漂亮的应用程序和网站。

无论是美感还是功能都比easyui更胜一筹,代码开源使用收费。

 

SyntacticSugar.dll:

功能齐全包含验证、通用扩展函数、类型转换、文件上传、以及大量C#语法糖的一款工具类库。

源码地址:https://github.com/sunkaixuan/SyntacticSugar

 

SqlSugar.dll:

是一款基于MSSQL的轻量级、高性能、简单易用的ORM框架

教程及源码下载地址: http://www.cnblogs.com/sunkaixuan/p/4649904.html

 

JQWidgetsSugar.dll  (本贴的重点)

基于jqwidgets.js 的C#封装类库 ,目前只完成了grid部分 ,我的所有GIT项目会在以后项目开发中持续更新

 

效果图:

 

C#代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using SqlSugar;
  7. using DAL;
  8. using JQWidgetsSugar;
  9. using Models;
  10. using SyntacticSugar;
  11. namespace NetJQWidgets.Controllers
  12. {
  13. public class GridController : Controller
  14. {
  15. public ActionResult Index()
  16. {
  17. var adp = new GridDataAdapterSource();
  18. adp.url = "/Grid/Data";
  19. var gc = new GridConfig();
  20. gc.gridbuttons = new List<GridButton>()
  21. {
  22. new GridButton(){ click="add", name="addbutton", icon="jqx-icon-plus", title="添加"},
  23. new GridButton(){ click="edit", name="editbutton", icon="jqx-icon-edit", title="编辑"},
  24. new GridButton(){ click="del", name="delbutton", icon="jqx-icon-delete", title="删除"}
  25. };
  26. gc.pageSize = 20;
  27. gc.width = "80%";
  28. gc.columns = new List<GridColumn>(){
  29. new GridColumn(){ text="编号", datafield="id", width="40px", cellsalign=AlignType.left,datatype=Datatype.dataint },
  30. new GridColumn(){ text="名称", datafield="name", cellsalign=AlignType.left,datatype=Datatype.datastring },
  31. new GridColumn(){ text="产品名", datafield="productname", cellsalign=AlignType.left,datatype=Datatype.datastring },
  32. new GridColumn(){ text="数量", datafield="quantity", cellsalign=AlignType.right , datatype=Datatype.dataint },
  33. new GridColumn(){ text="创建时间", datafield="date", cellsformat="yyyy-MM-dd",cellsalign=AlignType.right, datatype=Datatype.datadate
  34. }
  35. };
  36. var grid = JQXGrid.BindGrid("#netgrid", adp, gc);
  37. ViewBag.validationBind = ValidationSugar.GetBindScript("validate_key_grid_index");
  38. return View(grid);
  39. }
  40. [HttpDelete]
  41. public JsonResult Del(int id)
  42. {
  43. using (SqlSugarClient db = SugarDao.GetInstance())
  44. {
  45. ActionResultModel<string> model = new ActionResultModel<string>();
  46. model.isSuccess = db.Delete<GridTable>(id);
  47. model.respnseInfo = model.isSuccess ? "删除成功" : "删除失败";
  48. return Json(model);
  49. }
  50. }
  51. [HttpPost]
  52. public JsonResult Add(GridTable gt)
  53. {
  54. using (SqlSugarClient db = SugarDao.GetInstance())
  55. {
  56. string message = string.Empty;
  57. var isValid = ValidationSugar.PostValidation("validate_key_grid_index", out message);
  58. ActionResultModel<string> model = new ActionResultModel<string>();
  59. if (isValid)//后台验证数据完整性
  60. {
  61. model.isSuccess = db.Insert(gt) != DBNull.Value;
  62. model.respnseInfo = model.isSuccess ? "添加成功" : "添加失败";
  63. }
  64. else {
  65. model.isSuccess = false;
  66. model.respnseInfo = message;
  67. }
  68. return Json(model);
  69. }
  70. }
  71. [HttpPut]
  72. public JsonResult Edit(GridTable gt)
  73. {
  74. using (SqlSugarClient db = SugarDao.GetInstance())
  75. {
  76. ActionResultModel<string> model = new ActionResultModel<string>();
  77. string message = string.Empty;
  78. var isValid = ValidationSugar.PostValidation("validate_key_grid_index", out message);
  79. if (isValid)//后台验证数据完整性
  80. {
  81. model.isSuccess = db.Update<GridTable>(gt, it => it.id == gt.id);
  82. model.respnseInfo = model.isSuccess ? "编辑成功" : "编辑失败";
  83. }
  84. else {
  85. model.isSuccess = false;
  86. model.respnseInfo = message;
  87. }
  88. return Json(model);
  89. }
  90. }
  91. [OutputCache(Duration = 0)]
  92. public JsonResult Data(GridSearchParams pars)
  93. {
  94. using (SqlSugarClient db = SugarDao.GetInstance())
  95. {
  96. if (pars.sortdatafield == null) { //默认按id降序
  97. pars.sortdatafield = "id";
  98. pars.sortorder = "desc";
  99. }
  100. Sqlable sable = db.Sqlable().Form<GridTable>("g");//查询表的sqlable对象
  101. var model = JQXGrid.GetWidgetsSource<Models.GridTable>(sable, pars);//根据grid的参数自动查询
  102. return Json(model, JsonRequestBehavior.AllowGet);
  103. }
  104. }
  105. }
  106. }

 

Razor视图

  1. @{
  2. ViewBag.Title = "Index";
  3. Layout = "~/Views/Shared/_Layout.cshtml";
  4. }
  5. @using JQWidgetsSugar
  6. @section head{
  7. <script src="/Content/My97DatePickerBeta/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
  8. <link href="/Content/My97DatePickerBeta/My97DatePicker/skin/WdatePicker.css" rel="stylesheet"
  9. type="text/css" />
  10. <script src="/Content/jquery-validation-1.13.1/dist/jquery.validate.min.js" type="text/javascript"></script>
  11. <link href="/Content/jquery-validation-1.13.1/validation.sugar.css" rel="stylesheet"
  12. type="text/css" />
  13. <script src="/Content/jquery-validation-1.13.1/validation.sugar.js" type="text/javascript"></script>
  14. <script type="text/javascript">
  15. //添加
  16. function add(row) {
  17. save(row, true);
  18. }
  19. //编辑
  20. function edit(row) {
  21. save(row, false);
  22. }
  23. //删除
  24. function del(row) {
  25. if (row == null) {
  26. jqxAlert('请选择一条记录!')
  27. } else {
  28. jqxDelete({ gridSelector: "#netgrid",
  29. url: "/Grid/Del",
  30. data: { id: row.id }
  31. });
  32. }
  33. }
  34. function save(row, isAdd) {
  35. var isEdit = !isAdd;
  36. if (isEdit) {
  37. if (row == null) {
  38. jqxAlert('请选择一条记录!')
  39. return;
  40. }
  41. }
  42. //弹出框
  43. jqxWindow("#editbox", isAdd?"添加":"编辑", 400, "auto");
  44. //美化 button
  45. $("#editbox button").jqxButton();
  46. //取消事件
  47. $('#cancel').unbind();
  48. $('#cancel').on('click', function (e) {
  49. $("#editbox").jqxWindow("close")
  50. });
  51. if (isAdd) {
  52. //清空表单
  53. $("#frmtable").formClear();
  54. } else {
  55. //格日化日期
  56. row.date = $.convert.toDate(row.date, "yyyy-MM-dd")
  57. //通过JSON自动填充表单,也可以自已实现
  58. $("#frmtable").formFill({ data: row })
  59. }
  60. //确定事件
  61. $('#save').unbind();
  62. $('#save').on('click', function (e) {
  63. factory.ajaxSubmit(function () {
  64. var url = isAdd ? "/grid/add" : "/grid/edit";
  65. var type = isAdd ? "post" : "put";
  66. $("#frmtable").ajaxSubmit({
  67. url: url,
  68. type: type,
  69. success: function (msg) {
  70. if (msg.isSuccess == false) {
  71. jqxAlert(msg.respnseInfo);
  72. }
  73. $("#netgrid").jqxDataTable('updateBoundData');
  74. $("#editbox").jqxWindow("close")
  75. }, error: function (msg) {
  76. console.log(msg);
  77. }
  78. })
  79. });
  80. });
  81. }
  82. //绑定验证
  83. $(function () {
  84. window.factory = new validateFactory($("form"), "<img src=\"/Content/jquery-validation-1.13.1/error.png\" />");
  85. factory.init();
  86. });
  87. </script>
  88. @Html.Raw(Model)
  89. }
  90. <div id="netgrid">
  91. </div>
  92. <div id="editbox" class="hide">
  93. <div class="savetable">
  94. <form id="frmtable" class="form">
  95. <table style="table-layout: fixed; border-style: none;">
  96. <tr>
  97. <td align="right">
  98. 名称:
  99. </td>
  100. <td align="left">
  101. <input id="id" name="id" type="hidden" value="0" />
  102. <input id="name" name="name" type="text" />
  103. </td>
  104. </tr>
  105. <tr>
  106. <td align="right">
  107. 产品名:
  108. </td>
  109. <td align="left">
  110. <input id="productname" name="productname" type="text" />
  111. </td>
  112. </tr>
  113. <tr>
  114. <td align="right">
  115. 数量:
  116. </td>
  117. <td align="left">
  118. <input id="quantity" name="quantity" type="text" />
  119. </td>
  120. </tr>
  121. <tr>
  122. <td align="right">
  123. 时间:
  124. </td>
  125. <td align="left">
  126. <input id="date" class="Wdate" οnclick="WdatePicker()" name="date" type="text" />
  127. </td>
  128. </tr>
  129. <tr>
  130. <td>
  131. </td>
  132. <td>
  133. <br />
  134. <button id="save" type="button">
  135. 保存</button>
  136. <button style="margin-left: 5px;" type="button" id="cancel">
  137. 取消</button>
  138. </td>
  139. </tr>
  140. </table>
  141. </form>
  142. </div>
  143. </div>
  144. @Html.Raw(ViewBag.validationBind)

 

例子不是很难,就是最基本的增、删、查、改。

功能虽然简单但是也考虑到了很多问题比如: 前端加后端的双向验证、代码扩展性强、语法简洁、异常的处理等。 

 

DEMO下载地址:https://github.com/sunkaixuan/JQWidgetsSugar

 

  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/134987
推荐阅读
相关标签
  

闽ICP备14008679号