JQuery對(duì)ASP.NET MVC數(shù)據(jù)進(jìn)行更新刪除
以前學(xué)習(xí)ASP.NET MVC時(shí),學(xué)習(xí)與應(yīng)用,操作過數(shù)據(jù)顯示,添加,編輯,更新和刪除等功能。
很多方法是相通的,看自己是怎樣來進(jìn)行方便,快捷,高效率。
今天Insus.NET寫的練習(xí),是直接對(duì)綁定在Table的數(shù)據(jù)進(jìn)行更新,刪除。
在項(xiàng)目中,創(chuàng)建一個(gè)實(shí)體,也就是說,對(duì)數(shù)據(jù)庫時(shí)行通信,對(duì)數(shù)據(jù)進(jìn)行操作:
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();
}
在項(xiàng)目的控制器中:
創(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代碼:
運(yùn)行一下,看看效果:
上面是對(duì)數(shù)據(jù)進(jìn)行更新的功能,下面的實(shí)現(xiàn),是對(duì)Table內(nèi)的數(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>
上面標(biāo)記#4的jQuery代碼,即是刪除的核心功能:
運(yùn)行程序,看看刪除的效果:
刪除成功之后,我們不必重導(dǎo)向,只需要?jiǎng)h除這行html即可,來達(dá)到:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- asp.net mvc 從數(shù)據(jù)庫中讀取圖片的實(shí)現(xiàn)代碼
- asp.net mvc4 mysql制作簡(jiǎn)單分頁組件(部分視圖)
- 利用ASP.NET MVC+EasyUI+SqlServer搭建企業(yè)開發(fā)框架
- 使用jQuery向asp.net Mvc傳遞復(fù)雜json數(shù)據(jù)-ModelBinder篇
- ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解
- ASP.NET中MVC從后臺(tái)控制器傳遞數(shù)據(jù)到前臺(tái)視圖的方式
- Asp.net mvc 數(shù)據(jù)調(diào)用示例代碼
- ASP.NET MVC 數(shù)據(jù)驗(yàn)證及相關(guān)內(nèi)容
- ASP.NET Mvc開發(fā)之刪除修改數(shù)據(jù)
- ASP.NET中MVC傳遞數(shù)據(jù)的幾種形式總結(jié)
- ASP.NET Mvc開發(fā)之查詢數(shù)據(jù)
- asp.net實(shí)現(xiàn)的MVC跨數(shù)據(jù)庫多表聯(lián)合動(dòng)態(tài)條件查詢功能示例
- ASP.NET MVC使用EPPlus,導(dǎo)出數(shù)據(jù)到Excel中
相關(guān)文章
jQuery實(shí)現(xiàn)鼠標(biāo)可拖動(dòng)調(diào)整表格列寬度
這篇文章主要介紹了通過jQuery實(shí)現(xiàn)鼠標(biāo)可拖動(dòng)調(diào)整表格列寬度,需要的朋友可以參考下2014-05-05
基于jquery的手風(fēng)琴圖片展示效果實(shí)現(xiàn)方法
這篇文章主要介紹了基于jquery的手風(fēng)琴圖片展示效果實(shí)現(xiàn)方法,涉及jQuery基于事件實(shí)現(xiàn)改變css樣式的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
jquery操作select元素和option的實(shí)例代碼
這篇文章主要介紹了jquery操作select元素和option的實(shí)例代碼,感興趣的小伙伴們可以參考一下2016-02-02
jquery實(shí)現(xiàn)具有收縮功能的垂直導(dǎo)航菜單
這篇文章主要介紹了jquery實(shí)現(xiàn)具有收縮功能的垂直導(dǎo)航菜單點(diǎn)擊可以展開折疊的導(dǎo)航菜單,感興趣的小伙伴們可以參考一下2016-02-02
jquery序列化form表單使用ajax提交后處理返回的json數(shù)據(jù)
這篇文章主要介紹了jquery序列化form表單,使用ajax提交后處理返回的json數(shù)據(jù)的示例,需要的朋友可以參考下2014-03-03
用jquery的方法制作一個(gè)簡(jiǎn)單的導(dǎo)航欄
用jquery制作一個(gè)簡(jiǎn)單的導(dǎo)航欄,使用到了addClass及removeClass等方法,需要的朋友可以參考下2014-06-06









