asp.net mvc + dapper(ORM框架) + easyui框架简洁的信息管理项目
2024-10-18 21:20:30
1.目录结构:
2.效果图:
3.IndexController控制器:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json; namespace qrcodeMvcSystem.Controllers
{
public class IndexController : Controller
{
// GET: Index
public ActionResult Index()
{
return View();
} /// <summary>
/// datagrid数据绑定
/// </summary>
/// <param name="rows">每行显示的条数</param>
/// <param name="page">当前页</param>
/// <param name="sort">排序字段</param>
/// <param name="order">排序方式</param>
/// <param name="query">条件</param>
/// <returns></returns>
[HttpPost]
public ActionResult LoadList(string rows, string page, string sort, string query)
{
int count = ;
IList list = DBHelper.GetList1(Convert.ToInt32(rows), Convert.ToInt32(page), sort, ref count);
return Content(JsonConvert.SerializeObject(new
{
total = count,
rows = list
}));
}
/// <summary>
/// 修改添加数据
/// </summary>
/// <param name="goods"></param>
/// <returns></returns>
[HttpPost]
public ActionResult AcceptClick(Goods goods)
{
int isOk = default(int);
if(goods.ID!=)
{
isOk = DBHelper.Update(goods);
}
else
{
isOk = DBHelper.Insert(goods);
}
return Content(isOk.ToString());
}
/// <summary>
/// 查看详细信息
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost]
public ActionResult LoadForm(string id)
{
if (!string.IsNullOrEmpty(id))
return Json(DBHelper.GetEntity(id));
else
return null;
}
/// <summary>
/// 删除一条数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public ActionResult Del(string id)
{
if (!string.IsNullOrEmpty(id))
return Content(DBHelper.Delete(id).ToString());
else
return null;
}
}
}
4.index.cshtml
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
@*Easyui需要引入的文件*@
<script type="text/javascript" src="~/Content/jquery-easyui-1.4.5/jquery.min.js"></script>
<script type="text/javascript" src="~/Content/jquery-easyui-1.4.5/jquery.easyui.min.js"></script>
<link href="~/Content/jquery-easyui-1.4.5/themes/default/easyui.css" rel="stylesheet" />
<link href="~/Content/jquery-easyui-1.4.5/themes/icon.css" rel="stylesheet" />
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/messages_zh.js"></script>
<script type="text/javascript" src="~/Content/Site.js"></script>
<script type="text/javascript">
$(function () {
//初始化datagrid数据
InitGrid();
InitDialog();
$('#btnReload').click(function () {
$("#grid").datagrid("reload");
});
//ajax提交修改表单数据
$('#ok').click(function () {
$('#form').submit();
});
$('#cancel').click(function () {
$('#dd').dialog('close');
});
$('#add').click(function () {
$('#form')[].reset();
$('#ID').val("此字段自动生成.");
$('#dd').dialog('open');
});
$('#del').click(function () {
getAjax("../Index/Del", { id: $('#del_id').val() }, function (data) {
if ($('#del_id').val() == null || $('#del_id').val() == "")
return false;
if (data) {
$.messager.alert('提示', '操作成功!');
$('#dd').dialog('close');
$("#grid").datagrid("reload");
return true;
}
else {
$.messager.alert('提示', '操作失败!');
return false;
}
});
});
});
function InitGrid() {
$('#grid').datagrid({
url: '../Index/LoadList',
nowrap: true,//单行显示
autoRowHeight: false,
striped: false, //斑马纹
collapsible: true, //可折叠
pagination: true,
singleSelect: true,
border: true,
pageSize: ,
fit: true,
fitColumns: true, //自适应列宽
rownumbers: true,
columns: [[
{ title: '编号', field: 'ID', hidden: true },
{ title: '入库方式', field: 'Name', width: },
{ title: '计价方式', field: 'PriceWay', width: },
{ title: '计价公式', field: 'PriceFormula', width: },
]],
toolbar: '#tb',
onDblClickRow: function (rowIndex, rowData) {
getAjax("../Index/LoadForm",
{ id: rowData['ID'] }, function (data) {
var data = eval("(" + data + ")");
SetWebControls(data);
});
$('#dd').dialog('open');
},
onClickRow: function (index, row) {
$('#del_id').val(row['ID']);
}
})
var p = $('#grid').datagrid('getPager');
$(p).pagination({
beforePageText: '第',
afterPageText: '页 共 {pages} 页',
displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录',
}); }
function InitDialog() {
$('#dd').dialog({
title: '修改信息',
width: ,
height: ,
top: ($(window).height() - ) * 0.5, //居中
left: ($(window).width() - ) * 0.5,
closed: true,
cache: false,
modal: true,
buttons: '#dlg-buttons'
});
}
</script>
<script>
//表单验证和提交
$(function () {
$('#form').validate({
rules: {
Name: {
required: true
}
},
submitHandler: function (form) {
var postData = GetWebControls("#form");
console.log(postData);
getAjax("/Index/AcceptClick",
postData, function (data) {
if (data) {
alert(data);
$.messager.alert('提示', '操作成功!');
$('#dd').dialog('close');
$("#grid").datagrid("reload");
return true;
}
else {
$.messager.alert('提示', '操作失败!');
return false;
}
});
},
invalidHandler: function (form, validator) { //不通过回调
return false;
},
showErrors: function (errorMap, errorList) {
this.defaultShowErrors();
for (var i = ; i < errorList.length; i++) {
$(errorList[i].element).one("blur", function () {
$("label.error[for='" + (this.id ? this.id : this.name) + "']").remove();
});
}
}
});
});
</script>
</head>
<body style="margin:0;padding:0;">
<div>
<input id="del_id" type="hidden" name="del_id" value=" " />
<div style="position:fixed;width:100%;height:100%">
<table id="grid"></table>
</div>
<div id="tb" style="padding:3px">
<a id="add" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
<a id="del" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
<a id="btnReload" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reload'">刷新</a>
</div>
</div>
<div id="dd" class="easyui-dialog">
<form id="form" name="form" method="post">
<table style="margin:8px">
<tr>
<td>编号:</td>
<td><input type="text" id="ID" name="ID" value=" " disabled="disabled" /></td>
</tr>
<tr>
<td>入库方式:</td>
<td><input type="text" id="Name" name="Name" value="" class="required" /></td>
</tr>
<tr>
<td>计价方式:</td>
<td><input type="text" id="PriceWay" name="PriceWay" value=" " /></td>
</tr>
<tr>
<td>计价公式:</td>
<td><input type="text" id="PriceFormula" name="PriceFormula" value=" " /></td>
</tr>
</table>
<div id="dlg-buttons">
<a id="ok" href="#" class="easyui-linkbutton" iconcls="icon-ok">确定</a>
<a id="cancel" href="#" class="easyui-linkbutton" iconcls="icon-cancel">取消</a>
</div>
</form>
</div>
</body>
</html>
5.Goods模型类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; public class Goods
{
public int ID { get; set; }
public string Name { get; set; }
public string PriceWay { get; set; }
public string PriceFormula { get; set; }
}
6.数据库操作dapper框架
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Web;
using Dapper; public static class DBHelper
{
private static readonly string connString = "Data Source=.;Initial Catalog=qrab;Integrated Security=False;User ID=sa;Password=111111;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;";
//ConfigurationManager.ConnectionStrings["PharmacySystem"].ConnectionString;
private static IDbConnection _conn;
public static IDbConnection Conn
{
get
{
_conn = new SqlConnection(connString);
_conn.Open();
return _conn;
}
}
public static int Insert(Goods goods)
{
using (Conn)
{
string query = "insert into Goods(Name,PriceWay,PriceFormula)values(@Name,@PriceWay,@PriceFormula)";
return Conn.Execute(query,goods);
}
}
public static int Update(Goods goods)
{
using (Conn)
{
string query = "update Goods set Name=@Name,PriceWay=@PriceWay,PriceFormula=@PriceFormula where id=@ID";
return Conn.Execute(query,goods);
}
} public static int Delete(string id)
{
using (Conn)
{
string query = "delete from Goods where id=@id";
return Conn.Execute(query, new { id = id });
}
} public static IList<Goods> GetList()
{
using (Conn)
{
string query = "select * from Goods";
return Conn.Query<Goods>(query).ToList();
}
} public static Goods GetEntity(string id)
{
Goods goods;
string query = "select * from Goods where id=@id";
using (Conn)
{
goods = Conn.Query<Goods>(query, new { id = id }).SingleOrDefault();
return goods;
}
} public static IList GetList1(int rows, int page, string sort, ref int count)
{
int num1 = (page - ) * rows;
//int num1 = rows * page;
using (Conn)
{
string query = "select top "+rows+" * from Goods as b where b.id not in(select top "+num1+" id from Goods)";
count = Conn.Query<int>("select COUNT(1) from Goods As t").Single();
return Conn.Query<Goods>(query).ToList();
}
} }
最新文章
- Vuex2.0+Vue2.0构建备忘录应用实践
- 低版本GCC程序向高版本移植的兼容性问题
- 远程连接oracle服务器
- 使用AndroidStudio报错:INSTALL_FAILED_UPDATE_INCOMPATIBLE
- pushState()、popstate事件配合ajax实现浏览器前进后退页面局部刷新
- 删除自带OpenJDK
- Struts2 - Action no cache
- 在Ubuntu中用root帐号登录
- 决策树之ID3算法实现(python)
- jq模拟操作
- Excel根据字符串截取单元格部分内容
- 【xsy2479】counting 生成函数+多项式快速幂
- .net core中使用Type.GetType()从字符串获取类型遇到的问题
- summaryなな
- Ubuntu 16.04实现SSH无密码登录/免密登录/自动登录(ssh-keygen/ssh-copy-id)
- Oracle学习笔记之五sp1,PL/SQL之BULK COLLECT
- thuwc2018 爆炸记
- JSP + servlet 源码 实现文件的上传
- Linux命令之route
- redis_入门网址
热门文章
- JVM探秘2--详解内存溢出OutOfMemoryError异常
- java,利用Selenium调用浏览器,动态模拟浏览器事件,动态获取页面信息
- week_one-python基础 基本语法、流程控制
- 158A Next Round
- Html br 标签
- Docker Swarm bind 数据持久化
- 使用Angular2的Http发送AJAX请求
- vue中父子组件之间的传值、非父子组件之间的传值
- GIT 私有仓库 github项目提交失败 master ->; master (non-fast-forward)
- Win32汇编学习(5):绘制文本2