vs2017 + miniUI + dapper 添加、修改、删除、查询操作
a、数据库表引用先前建立的company 公司信息表。
b、建立文件:
views 》 home 》 company.cshtml(新建文件) ,代码如下(直接复制即可)
@{
layout = layout = "~/views/shared/_layout.cshtml"; ;
}
<div style="width:100%;">
<div class="mini-toolbar" style="border-bottom:0;padding:0px;">
<table style="width:100%;">
<tr>
<td style="width:100%;">
<a class="mini-button" iconcls="icon-add" onclick="add()">添加</a>
</td>
<td style="white-space:nowrap;">
<input id="key" name="key" class="mini-textbox" emptytext="请输入姓名" style="width:150px;" />
<a class="mini-button" onclick="search()">查询</a>
</td>
</tr>
</table>
</div>
</div>
<div id="datagrid1" class="mini-datagrid" style="width:100%;height:280px;" allowresize="true" pagesize="10"
url="/home/companylist" idfield="id" multiselect="true" pagesize="20" showpager="true">
<div property="columns">
<!--<div type="indexcolumn"></div> -->
<div type="checkcolumn"></div>
<div field="companyname" width="120" headeralign="center" allowsort="true">公司名称</div>
<div field="address" width="120" headeralign="center" allowsort="true">地址</div>
<div field="mobile" width="120" headeralign="center" allowsort="true">电话</div>
<div name="action" width="100" renderer="ongenderrenderer" headeralign="center">操作</div>
</div>
</div>
<script>
mini.parse();
var grid = mini.get("datagrid1");
grid.load();
grid.on("drawcell", function (e) {
var record = e.record,
column = e.column,
field = e.field,
value = e.value;
//action列,超连接操作按钮
if (column.name == "action") {
e.cellstyle = "text-align:center";
e.cellhtml = '<a class="mini-button" style="width:60px;" href="javascript:edit(\'' + record.id + '\')">修改</a> '
+ '<a class="mini-button" style="width:60px;" href="javascript:del(\'' + record.id + '\')" >删除</a>'
}
});
function search() {
var key = mini.get("key").value;
grid.load({ key: key });
}
$("#key").bind("keydown", function (e) {
if (e.keycode == 13) {
search();
}
});
function del(rowid) {
if (rowid.length > 0) {
if (confirm("确定删除选中记录?")) {
grid.loading("操作中,请稍后......");
$.ajax({
url: "/home/delete?id=" + rowid,
type: "post",
success: function (text) {
grid.reload();
},
error: function () {
}
});
}
} else {
alert("请选中一条记录");
}
}
//添加、修改
function edit(rowid) {
mini.open({
targetwindow: window,
url: "/home/companyedit?id=" + rowid,
title: "设置条件", width: 800, height: 600,
onload: function () {
var iframe = this.getiframeel();
var data = { action: "new" };
iframe.contentwindow.opencontext = { parent: window, popwin: this };
},
ondestroy: function (action) {
loading();
grid.reload();
}
});
}
//新建
function add() {
mini.open({
targetwindow: window,
url: "/home/companyedit",
title: "添加菜单", width: 700, height: 300,
onload: function () {
var iframe = this.getiframeel();
var data = { action: "new" };
loading();
iframe.contentwindow.opencontext = { parent: window, popwin: this };
},
ondestroy: function (action) {
grid.reload();
}
});
}
function loading() {
mini.mask({
el: document.body,
cls: 'mini-mask-loading',
html: '加载中...'
});
settimeout(function () {
mini.unmask(document.body);
}, 1000);
}
</script>
views 》 home 》companyedit.cshtml(新建文件) ,代码如下(直接复制即可)
@model miniui_dapper.controllers.company
@{
viewbag.title = "companyedit";
layout = "~/views/shared/_layout.cshtml";
}
<form id="form1" method="post">
<div class="mini-fit'">
<input name="id" id="id" class="mini-hidden" value="@model.id" />
<table border="0" cellpadding="2" cellspacing="0" class="edittable" style="width: 100%">
<tr>
<td class="tdlabel" style="width: 30%;"> 公司名称:</td>
<td class="tdtext" style="width: 70%;"><input class="mini-textbox" id="companyname" name="companyname" style="width: 200px;" value="@model.companyname"></td>
</tr>
<tr>
<td class="tdlabel" style="width: 30%;"> 地址: </td>
<td class="tdtext" style="width: 70%;"> <input class="mini-textbox" id="address" name="address" style="width: 200px;" value="@model.address"> </td>
</tr>
<tr>
<td class="tdlabel" style="width: 30%;"> 电话: </td>
<td class="tdtext" style="width: 70%;"> <input class="mini-textbox" id="mobile" name="mobile" style="width: 200px;" value="@model.mobile"> </td>
</tr>
</table>
<div style="text-align:center;padding:10px;">
<a class="mini-button" onclick="onok" style="width:60px;margin-right:20px;">确定</a>
<a class="mini-button" onclick="oncancel" style="width:60px;">取消</a>
</div>
</div>
</form>
<script type="text/javascript">
mini.parse();
var form = new mini.form("form1");
function savedata() {
var form = new mini.form("#form1");
var data = form.getdata(); //获取表单多个控件的数据
//var json = mini.encode(data); //序列化成json
$.ajax({
url: "/home/companysave",
type: "post",
data: data,
success: function (text) {
//alert("提交成功,返回结果:" + text);
//grid.reload();
closewindow("提交成功");
}
});
}
function getdata() {
var o = form.getdata();
return o;
}
function closewindow(action) {
if (action == "close" && form.ischanged()) {
if (confirm("数据被修改了,是否先保存?")) {
return false;
}
}
if (window.closeownerwindow) return window.closeownerwindow(action);
else window.close();
}
function onok(e) {
savedata();
}
function oncancel(e) {
closewindow("cancel");
}
</script>
controllers》homecontroller.cs(控制器新建方法) ,代码如下(直接复制即可)
#region 公司管理
/// <summary>
///
/// </summary>
/// <param name="pageindex"></param>
/// <param name="pagesize"></param>
/// <param name="key">查询关键字,当前匹配公司名称</param>
/// <returns></returns>
[httppost]
public actionresult companylist(int? pageindex, int? pagesize,string key)
{
using (var conn = new mysqlconnection(connectionstring))
{
pageindex = pageindex ?? 0;
pagesize = pagesize ?? 10;
string query = "";
if (key != null && key.trim() != "")
query = "select * from company" + " where companyname like '%"+ key +"%'";
else
query = "select * from company";
var result = conn.query<company>(query).tolist();
var total = result.count();
var list = result
.orderbydescending(d => d.id).skip((pageindex * pagesize).value)
.take((pagesize).value).select(d => new
{
d.id,
d.companyname,
d.address,
d.mobile,
d.link,
d.intodate
}).tolist();
return json(new { total = total, data = list }, jsonrequestbehavior.allowget);
}
}
[httpget]
[display(description = "加载公司信息")]
public actionresult companyedit(int? id)
{
//点击编辑页面,如果id=0 则是增加操作,如果id>0则是修改操作。
using (var conn = new mysqlconnection(connectionstring))
{
company n;
if ((id ?? 0) == 0)
n = new company();
else
{
n = conn.query<company>("select * from company where id=" + id).firstordefault();
if (n == null)
return view("无公司信息");
}
return view(n);
}
}
[httppost]
[display(description = "保存公司信息")]
public actionresult companysave(company company)
{
try
{
using (var conn = new mysqlconnection(connectionstring))
{
if (company.id == 0)
{
var query = " insert into `company`(`companyname`, `address`, `mobile`, `link`) values (@companyname, @address, @mobile, @link) ;";
var result = conn.query<company>(query,company).firstordefault();
return json(new { @return = 1 }, jsonrequestbehavior.allowget);
}
else
{
var query = "update `company` set `companyname` = @companyname, `address` = @address, `mobile` = @mobile, `link` = @link where `id` = "+ company.id + ";";
var result = conn.query<company>(query, company).firstordefault();
if (result == null)
return json(new { @return = 1 }, jsonrequestbehavior.allowget);
else
return json(new { @return = 1 }, jsonrequestbehavior.allowget);
}
}
}
catch (exception ex)
{
return json(new { @return = 1 }, jsonrequestbehavior.allowget);
}
return json(new { @return = 1 }, jsonrequestbehavior.allowget);
}
/// <summary>
/// 根据表主键id进行删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public actionresult delete(int id)
{
using (var conn = new mysqlconnection(connectionstring))
{
conn.execute("delete from company where id=" + id);
return json(new { @return = 1 }, jsonrequestbehavior.allowget);
}
}
#endregion
修改左菜单导航的json文件
到此完成数据库的增删改查操作。运行一下看看结果。
可加qq群索要源码或交流:547765059
上一篇: java sort方法_Java 排序
下一篇: ios 自定义加载动画效果
推荐阅读
-
vs2017 + miniUI + dapper 添加、修改、删除、查询操作
-
cmd 命令行模式操作数据库 添加查询 修改 删除 ( 表 字段 数据)
-
PHP中MongoDB数据库的连接、添加、修改、查询、删除等操作实例
-
yii2.0框架数据库操作简单示例【添加,修改,删除,查询,打印等】
-
cmd 命令行模式操作数据库 添加查询 修改 删除 ( 表 字段 数据)
-
Laravel之DB类操作数据库(创建表、添加字段、创建控制器、添加数据、修改数据、删除数据、显示查询数据、返回ID的方法getId)
-
PHP中MongoDB数据库的连接、添加、修改、查询、删除等操作实例
-
vs2017 + miniUI + dapper 添加、修改、删除、查询操作
-
Jquery EasyUI的添加,修改,删除,查询等基本操作介绍_jquery
-
MySQL_1 关系型数据库的存储结构 数据库的添加,修改,删除,查询操作 数据库表的添加,修改,删除,查询操作 数据库表记录的添加,修改,删除,查询