.net core 添加,显示,查询,详情,删除

Api(dapper):

.net core 添加,显示,查询,详情,删除

Dal :idal

List<ManageInfomodel> Show();
ManageInfomodel Fant(int uid);

int Del(int id);
int Update(int id,int state);
int UAdd(ManageInfomodel m);
//下拉框
List<Classinfomodel> LeiShow();

dal:

public class Userdal : IUserdal
{
SqlConnection coon = new SqlConnection("Data Source=.;Initial Catalog=bluewhale;Integrated Security=True");
public List<ManageInfomodel> Show()
{
string sql = string.Format($"select m.*,c.ClassName,c.CId,c.ClassId from manageInfo m join ClassInfo c on m.ClassId = c.ClassId");
return coon.Query<ManageInfomodel>(sql).ToList();
}


/// <summary>
/// 反填
/// </summary>
/// <param name="uid"></param>
/// <returns></returns>

public ManageInfomodel Fant(int uid)
{
var list = Show();
var mod = list.Where(m => m.MId == uid).FirstOrDefault();
return mod;
}

public int Del(int id)
{
string sql = string.Format($"delete from manageInfo where MId={id}");
return coon.Execute(sql);
}

public int Update(int id,int state)
{
using (SqlConnection coon = new SqlConnection("Data Source=.;Initial Catalog=bluewhale;Integrated Security=True"))
{
string sql = string.Format($"update manageInfo set Mastate=Mastate-1 where MId=" + id);
return coon.Execute(sql);
}
}

public int UAdd(ManageInfomodel m)
{
using (SqlConnection coon = new SqlConnection("Data Source=.;Initial Catalog=bluewhale;Integrated Security=True"))
{
return coon.Execute($"insert into manageInfo values(‘{m.MName}‘,{m.ClassId},‘{m.UserName}‘,‘{m.InfoPhonn}‘,{m.ManSum},‘{m.Mastate}‘,‘{m.Mremark}‘)");
}
}

public List<Classinfomodel> LeiShow()
{
string sql = string.Format($"select * from ClassInfo");
return coon.Query<Classinfomodel>(sql).ToList();
}

 

controller:

[Route("api/[controller]/[Action]")]
[ApiController]
public class UserController : ControllerBase
{
private IUserdal _dal;
public UserController (IUserdal dal)
{
_dal = dal;
}
/// <summary>
/// 显示查询
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
[HttpGet]
public List<ManageInfomodel> Show(string name="")
{
var list = _dal.Show();
if (name != null)
{
list = list.Where(s => s.MName.Contains(name)).ToList();
}
return list;
}
/// <summary>
/// 反填
/// </summary>
/// <param name="uid"></param>
/// <returns></returns>
[HttpGet]
public ManageInfomodel Fant(int uid)
{
return _dal.Fant(uid);
}
[HttpPost]
public int Del(int id)
{
return _dal.Del(id);
}
[HttpPost]
public int Update(int id,int state )
{
if (state==0)
{
_dal.Update(id, 1);
}
else
{
_dal.Update(id,0);
}
return _dal.Update(id,state);
}
[HttpPost]
public int UAdd([FromForm]ManageInfomodel m)
{
return _dal.UAdd(m);
}
[HttpGet]
public List<Classinfomodel> LeiShow()
{
return _dal.LeiShow();.net core 添加,显示,查询,详情,删除
}

 

 

MVC: controller:

public IActionResult Index()
{
return View();
}
public IActionResult Show()
{
return View();
}
public IActionResult Fant(int MId)
{
ViewBag.Sid = MId;
return View();
}
public ActionResult Update(int id)
{
ViewBag.Sid = id;
return View();
}
public ActionResult UAdd1(int id)
{
ViewBag.Sid = id;
return View();
}
public ActionResult LeiShow()
{
return View();
}

 

//显示

@{
ViewData["Title"] = "Show";
}
<script src="~/layui-v2.5.6/layui-v2.5.6/layui/layui.js"></script>
<link href="~/layui-v2.5.6/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<h2>Show</h2>

部门:<input id="TextName" type="text" placeholder="查询部门" />
@*<i class="layui-icon layui-icon-search" style="font-size: 30px; color: #1E9FFF;"></i>*@
<input id="Button1" type="button" value="查询" onclick="getshow()" style="background-color:#0094ff" />
<input id="Hidden1" type="hidden" value="@ViewBag.Sid" />
<table class="table table-bordered" id="table">
<tr>
<td>编号</td>
<td>组织机构层级</td>
<td>分类</td>
<td><i class="layui-icon layui-icon-username" style="font-size: 20px; color: #1E9FFF;"></i>主负责人</td>
<td><i class="layui-icon layui-icon-cellphone" style="font-size: 20px; color: #1E9FFF;"></i> 电话</td>
<td>人数</td>
<td>状态</td>
<td>备注</td>
<td>操作</td>
</tr>
<tbody id="tbod"></tbody>
</table>


<script>

$(function () {
getshow();
});
function getshow() {
var sname = $("#TextName").val();
$.ajax({
url: "http://localhost:59632/api/user/show?name=" + sname,
datatype: "json",
type: "get",
success: function (data) {
console.log(data);
$("table tr:gt(0)").remove();
$(data).each(function () {
var str = ‘<tr>‘
+ ‘<td>‘ + this.mId + ‘</td>‘
+ ‘<td>‘ + this.mName + ‘</td>‘
+ ‘<td>‘ + this.className + ‘</td>‘
+ ‘<td>‘ + this.userName + ‘</td>‘
+ ‘<td>‘ + this.infoPhonn + ‘</td>‘
+ ‘<td>‘ + ‘<i class="layui-icon layui-icon-username"></i> ‘ + (this.manSum) + ‘</td>‘
+ ‘<td>‘ + ‘<i class="layui-icon layui-icon-circle-dot"style="font-size: 10px; color: green;"></i> ‘ + (this.mastate ? "正常" : "异常") + ‘</td>‘
+ ‘<td>‘ + this.mremark + ‘</td>‘
+ ‘<td><input id="Button1" type="button" value="详情" onclick="showModalPopup(‘ + this.mId + ‘)"/><input id="Button1" type="button" value="删除" onclick="Del(‘ + this.mId + ‘)"/><input id="Button1" type="button" value="停用" onclick="up(‘ + this.mId + ‘)"/><input id="Button1" type="button" value="添加" onclick="UAdd(‘ + this.mId + ‘)"/></td>‘
+ ‘</tr>‘;
$("#tbod").append(str);
});
}
})
}

function Del(sid) {
if (confirm("确认删除吗")) {
$.ajax({
url: "http://localhost:59632/api/user/Del?id=" + sid,
type: "post",


datatype: "json",
success: function (d) {
if (d > 0) {
alert("删除成功!");
Show();
}
else {
alert("删除失败");
}
}
})
}
}


function showModalPopup(sid) {
location.href = ‘/Default/Fant?mId=‘ + sid;
}
function UAdd() {
location.href = ‘/Default/UAdd1‘;

}
function up(sid) {
if (confirm("确认停用")) {
$.ajax({
url: "http://localhost:59632/api/user/Update?id=" + sid,
type: "post",


datatype: "json",
success: function (d) {
if (d > 0) {
alert("停用成功");
location.href = ‘/Default/Show‘;
}
else {
alert("停用失败");
return;

 


}
}
})
}
}

//function Del(zid) {
// location.href = ‘/Default/Del?mId=‘ + zid;
//}

 


</script>

 

//详情

@{
Layout = null;
}

<script src="~/lib/jquery/dist/jquery.js"></script>
<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Fant</title>
</head>
<body>
<input id="Hidden1" type="hidden" value="@ViewBag.Sid" />
<table>
<tr>
<td>编号:</td>
<td><input id="Textuid" type="text" /></td>
</tr>
<tr>
<td>部门名称:</td>
<td><input id="Textbu" type="text" /></td>
</tr>
<tr>
<td>负责人:</td>
<td><input id="Textpeo" type="text" /></td>
</tr>
<tr>
<td>手机号:</td>
<td><input id="Textphon" type="text" /></td>
<tr>
<td>分类:</td>
<td><input id="Textlei" type="text" /></td>
</tr>
<tr>
<td>备注:</td>
<td><input id="Textzhu" type="text" /></td>
</tr>

</table>
</body>
</html>
<script>
$(function () {
var strid = $("#Hidden1").val();
$.ajax({
url: "http://localhost:59632/api/user/Fant?uid=" + strid,
datatype: "json",
type: "get",
success: function (data) {
$("#Textuid").val(data.mId);
$("#Textbu").val(data.mName);
$("#Textpeo").val(data.uName);
$("#Textphon").val(data.phone);
$("#Textlei").val(data.className);
$("#Textzhu").val(data.mremark);

}
})
})

</script>

 

//添加


@{
ViewData["Title"] = "UAdd1";
}

<h2>UAdd1</h2>

<script src="~/lib/jquery/dist/jquery.js"></script>
<div>
<table class="table table-bordered">
<tr></tr>
<tr>
<td>组织机构层级:<input id="Text1" type="text" /></td>
</tr>
<tr>
<td>
分类:<select id="Select2">
<option></option>

</select>
</td>
</tr>
<tr>
<td>
主负责人:<input id="Text2" type="text" />
</td>
</tr>
<tr>
<td>

电话:<input id="Text3" type="text" />
</td>
</tr>
<tr>
<td>
人数:<input id="Text4" type="text" />
</td>
</tr>
<tr>
<td>
状态:<input id="Radio1" type="radio" name="1" checked="checked" />正常
<input id="Radio1" type="radio" name="1" />停用
</td>
</tr>
<tr>
<td>
备注:<input id="Text6" type="text" style="height:70px; width:300px" />
</td>
</tr>
<tr>
<td>
<input id="Button1" type="button" value="添加" onclick="zuzAdd()" />
</td>
</tr>
</table>
</div>
<script>
$(function () {
dropClass();
});
//下拉框
function dropClass() {
$.ajax({
url: "http://localhost:59632/api/user/LeiShow",
dataType: "json",
type: "get",
success: function (data) {
$("#Select2").empty();
$(data).each(function () {
var str = ‘<option values=‘ + this.cId + ‘>‘ + this.className + ‘</option>‘;
$("#Select2").append(str);
})
}
})
}
function Ti() {
var obj = {
MName: $("#txtMName").val(),
ClassId: $("#Textcid").val(),
UserName: $("#Textusername").val(),
InfoPhonn: $("#Textbeshi").val(),
ManSum: $("#Textsum").val(),
Mastate: $("#Text1").val(),
Mremark: $("#Textmremark").val()
};
$.ajax({
url: "http://localhost:59632/api/user/UAdd",
type: "post",
data: obj,
datatype: "json",
success: function (data) {
if (data > 0) {
alert("添加成功");
location.href = "/Default/Show";
}
else {
alert("提交失败");
}
}
})
}
function zuzAdd() {
var obj = {
MName: $("#Text1").val(),
UserName: $("#Text2").val(),
ClassId: $("#Select2").val(),
InfoPhonn: $("#Text3").val(),
Radio1: $("input[name=1]").val(),
ManSum: $("#Text4").val(),
Mremark: $("#Text6").val(),
};
if (obj.MName == "") {
alert("组织机构不能空");
return;
}
if (obj.UserName == "") {
alert("负责人不能空");
return;
}
if (obj.InfoPhonn == "") {
alert("电话不能空");
return;
}
if (obj.ManSum == "") {
alert("人数不能空");
return;
}
if (obj.MName == "") {
alert("组织机构不能空");
return;
}
if (obj.Mremark == "") {
alert("备注不能空");
return;
}
$.ajax({
url: "http://localhost:59632/api/user/UAdd",
type: "post",
data: obj,
dataType: "json",
success: function (data) {
if (data > 0) {
alert("添加成功");
getshow();
}
else {
alert("添加失败");
return;
}
}
})
}
function No() {
location.href = "/Default/Show";
}
</script>

 

.net core 添加,显示,查询,详情,删除

上一篇:新版chrome中非https无法打开摄像头


下一篇:Newtonsoft.Json 去掉\r\n