JQuery對ASP.NET MVC資料進行更新刪除_jquery

來源:互聯網
上載者:User

以前學習ASP.NET MVC時,學習與應用,操作過資料顯示,添加,編輯,更新和刪除等功能。
很多方法是相通的,看自己是怎樣來進行方便,快捷,高效率。
今天Insus.NET寫的練習,是直接對綁定在Table的資料進行更新,刪除。
在項目中,建立一個實體,也就是說,對資料庫時行通訊,對資料進行操作:

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();  }

在項目的控制器中:

建立視圖,並綁定資料:

@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代碼:

運行一下,看看效果:


上面是對資料進行更新的功能,下面的實現,是對Table內的資料刪除。

@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即可,來達到:

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.