BootstrapTable+KnockoutJS自定义T4模板快速生成增删改(2)

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using TestKO.Models; namespace TestKO.Controllers { public class UserController : Controller { public ActionResult Index() { return View(); } public ActionResult Edit(User model) { return View(model); } [HttpGet] public JsonResult Get(int limit, int offset) { return Json(new { }, JsonRequestBehavior.AllowGet); } //新增实体 [HttpPost] public JsonResult Add(User oData) { UserModel.Add(oData); return Json(new { }, JsonRequestBehavior.AllowGet); } //更新实体 [HttpPost] public JsonResult Update(User oData) { UserModel.Update(oData); return Json(new { }, JsonRequestBehavior.AllowGet); } //删除实体 [HttpPost] public JsonResult Delete(List<User> oData) { UserModel.Delete(oData); return Json(new { }, JsonRequestBehavior.AllowGet); } } }

2、KoIndex.cs.t4

这个模板主要用于生成列表页面,大致代码如下:

<#@ template language="C#" HostSpecific="True" #> <#@ output extension=".cshtml" #> <#@ include file="Imports.include.t4" #> <# // The following chained if-statement outputs the file header code and markup for a partial view, a view using a layout page, or a regular view. if(IsPartialView) { #> <# } else if(IsLayoutPageSelected) { #> @{ ViewBag.Title = "<#= ViewName#>"; <# if (!String.IsNullOrEmpty(LayoutPageFile)) { #> Layout = "<#= LayoutPageFile#>"; <# } #> } <# } else { #> @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta content="width=device-width" /> <title><#= ViewName #></title> <link href="https://www.jb51.net/~/Content/bootstrap/css/bootstrap.min.css" /> <link href="https://www.jb51.net/~/Content/bootstrap-table/bootstrap-table.min.css" /> <script src="https://www.jb51.net/~/scripts/jquery-1.9.1.min.js"></script> <script src="https://www.jb51.net/~/Content/bootstrap/js/bootstrap.min.js"></script> <script src="https://www.jb51.net/~/Content/bootstrap-table/bootstrap-table.min.js"></script> <script src="https://www.jb51.net/~/Content/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script> <script src="https://www.jb51.net/~/scripts/knockout/knockout-3.4.0.min.js"></script> <script src="https://www.jb51.net/~/scripts/knockout/extensions/knockout.mapping-latest.js"></script> <script src="https://www.jb51.net/~/scripts/extensions/knockout.index.js"></script> <script src="https://www.jb51.net/~/scripts/extensions/knockout.bootstraptable.js"></script> <script type="text/javascript"> $(function () { var viewModel = { bindId: "div_index", tableParams : { url : "/<#=ViewDataTypeShortName#>/Get", pageSize : 2, }, urls : { del : "/<#=ViewDataTypeShortName#>/Delete", edit : "/<#=ViewDataTypeShortName#>/Edit", add : "/<#=ViewDataTypeShortName#>/Edit", }, queryCondition : { } }; ko.bindingViewModel(viewModel); }); </script> </head> <body> <# PushIndent(" "); } #> <div> <button data-bind="click:addClick" type="button"> <span aria-hidden="true"></span>新增 </button> <button data-bind="click:editClick" type="button"> <span aria-hidden="true"></span>修改 </button> <button data-bind="click:deleteClick" type="button"> <span aria-hidden="true"></span>删除 </button> </div> <table data-bind="bootstrapTable:bootstrapTable"> <thead> <tr> <th data-checkbox="true"></th> <# IEnumerable<PropertyMetadata> properties = ModelMetadata.Properties; foreach (PropertyMetadata property in properties) { if (property.Scaffold && !property.IsPrimaryKey && !property.IsForeignKey) { #> <th data-field="<#= GetValueExpression(property) #>"><#= GetValueExpression(property) #></th> <# } }#> </tr> </thead> </table> <# // The following code closes the tag used in the case of a view using a layout page and the body and html tags in the case of a regular view page #> <# if(!IsPartialView && !IsLayoutPageSelected) { ClearIndent(); #> </body> </html> <# } #> <#@ include file="ModelMetadataFunctions.cs.include.t4" #>

添加一个视图Index,然后选择这个模板

BootstrapTable+KnockoutJS自定义T4模板快速生成增删改

得到的页面内容

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzxxgd.html