A、数据库表引用先前建立的company 公司信息表。
B、建立文件:
views 》 Home 》 Company.cshtml****(新建文件) ,代码如下(直接复制即可)
@{
Layout = Layout = "~/Views/Shared/_Layout.cshtml"; ;
}
views 》 Home 》CompanyEdit.cshtml****(新建文件) ,代码如下(直接复制即可)
@model MiniUi_Dapper.Controllers.Company
@{
ViewBag.Title = "CompanyEdit";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Controllers》HomeController.cs(控制器新建方法) ,代码如下(直接复制即可)
#region 公司管理
///
///
///
///
///
/// 查询关键字,当前匹配公司名称
///
[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
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
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
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
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);
}
///
/// 根据表主键Id进行删除
///
///
///
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