JQuery對ASP.NET MVC數(shù)據(jù)進行更新刪除
以前學習ASP.NET MVC時,學習與應用,操作過數(shù)據(jù)顯示,添加,編輯,更新和刪除等功能。
很多方法是相通的,看自己是怎樣來進行方便,快捷,高效率。
今天Insus.NET寫的練習,是直接對綁定在Table的數(shù)據(jù)進行更新,刪除。
在項目中,創(chuàng)建一個實體,也就是說,對數(shù)據(jù)庫時行通信,對數(shù)據(jù)進行操作:
public IEnumerable<ToolLocation> GetAllToolLocations()
{
sp.ConnectionString = DB.ConnectionString;
sp.Parameters = null;
sp.ProcedureName = "usp_ToolLocation_GetAll";
DataTable dt = sp.ExecuteDataSet().Tables[0];
return dt.ToList<ToolLocation>();
}
public void Update(ToolLocation tl)
{
List<Parameter> param = new List<Parameter>() {
new Parameter("@ToolLocation_nbr", SqlDbType.SmallInt, 2, tl.ToolLocation_nbr),
new Parameter("@LocationName",SqlDbType.NVarChar,-1,tl.LocationName),
new Parameter("@Description",SqlDbType.NVarChar,-1,tl.Description),
new Parameter("@IsActive",SqlDbType.Bit,1,tl.IsActive)
};
sp.ConnectionString = DB.ConnectionString;
sp.Parameters = param;
sp.ProcedureName = "usp_ToolLocation_Update";
sp.Execute();
}
public void Delete(ToolLocation tl)
{
List<Parameter> param = new List<Parameter>() {
new Parameter("@ToolLocation_nbr", SqlDbType.SmallInt, 2, tl.ToolLocation_nbr)
};
sp.ConnectionString = DB.ConnectionString;
sp.Parameters = param;
sp.ProcedureName = "usp_ToolLocation_Delete";
sp.Execute();
}
在項目的控制器中:
創(chuàng)建視圖,并綁定數(shù)據(jù):
@using Insus.NET.Models;
@model IEnumerable<ToolLocation>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Edit</title>
<link href="~/Content/css/table.css" rel="stylesheet" />
<script src="~/Scripts/jquery-2.2.1.js"></script>
</head>
<body>
<div>
<table>
<tr>
<td>ToolLocation_nbr</td>
<td>LocationName</td>
<td>Description</td>
<td>IsActive</td>
<td></td>
</tr>
@foreach (var tl in Model)
{
<tr>
<td>@tl.ToolLocation_nbr<input id="Hidden1" type="hidden" value="@tl.ToolLocation_nbr" /></td>
<td>@Html.TextBox("LocationName", tl.LocationName)</td>
<td>@Html.TextBox("Description", tl.Description) </td>
<td>@Html.CheckBox("IsActive", tl.IsActive)</td>
<td>
<input class="Update" type="button" title="Update" value="Update" />
</td>
</tr>
}
</table>
</div>
</body>
</html>
Source Code
上面步驟#4的jQuery代碼:
運行一下,看看效果:
上面是對數(shù)據(jù)進行更新的功能,下面的實現(xiàn),是對Table內的數(shù)據(jù)刪除。
@using Insus.NET.Models;
@model IEnumerable<ToolLocation>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Delete</title>
<link href="~/Content/css/table.css" rel="stylesheet" />
<script src="~/Scripts/jquery-2.2.1.js"></script>
</head>
<body>
<div>
<table>
<tr>
<td>ToolLocation_nbr</td>
<td>LocationName</td>
<td>Description</td>
<td>IsActive</td>
<td></td>
</tr>
@foreach (var tl in Model)
{
<tr>
<td>@tl.ToolLocation_nbr<input id="Hidden1" type="hidden" value="@tl.ToolLocation_nbr" /></td>
<td>@tl.LocationName</td>
<td>@tl.Description</td>
<td>@Html.CheckBox("IsActive", tl.IsActive, new { disabled = "disabled" })</td>
<td>
<input class="Delete" type="button" title="Delete" value="Delete" />
</td>
</tr>
}
</table>
</div>
</body>
</html>
上面標記#4的jQuery代碼,即是刪除的核心功能:
運行程序,看看刪除的效果:
刪除成功之后,我們不必重導向,只需要刪除這行html即可,來達到:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- asp.net mvc 從數(shù)據(jù)庫中讀取圖片的實現(xiàn)代碼
- asp.net mvc4 mysql制作簡單分頁組件(部分視圖)
- 利用ASP.NET MVC+EasyUI+SqlServer搭建企業(yè)開發(fā)框架
- 使用jQuery向asp.net Mvc傳遞復雜json數(shù)據(jù)-ModelBinder篇
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- ASP.NET中MVC從后臺控制器傳遞數(shù)據(jù)到前臺視圖的方式
- Asp.net mvc 數(shù)據(jù)調用示例代碼
- ASP.NET MVC 數(shù)據(jù)驗證及相關內容
- ASP.NET Mvc開發(fā)之刪除修改數(shù)據(jù)
- ASP.NET中MVC傳遞數(shù)據(jù)的幾種形式總結
- ASP.NET Mvc開發(fā)之查詢數(shù)據(jù)
- asp.net實現(xiàn)的MVC跨數(shù)據(jù)庫多表聯(lián)合動態(tài)條件查詢功能示例
- ASP.NET MVC使用EPPlus,導出數(shù)據(jù)到Excel中
相關文章
省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼
這篇文章主要為大家詳細紹了省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
webuploader模態(tài)框ueditor顯示問題解決方法
這篇文章主要為大家詳細介紹了webuploader模態(tài)框ueditor顯示問題的解決,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
jquery用ajax方式從后臺獲取json數(shù)據(jù)后如何將內容填充到下拉列表
從后臺獲取json數(shù)據(jù),將內容填充到下拉列表,頁面做如何處理,接下來,通過本篇文章給大家實例講解jquery用ajax方式從后臺獲取json數(shù)據(jù)后如何將內容填充到下拉列表,需要的朋友可以參考下2015-08-08









