.Net Core下使用Ajax,並傳送引數到controllers

CMD2018發表於2018-06-13

可以使用URL拼接方式方法傳參

.cshtml部分

 1 @section Scripts{
 2     @{ await Html.RenderPartialAsync("_ValidationScriptsPartial");}
 3     <script type="text/javascript" language="JavaScript">
 4         var SetStatClicked;
 5         $(document).ready(function () {
        SetStatClicked = function (strType, strBillID) {
6 $.ajax( 7 { 8 type: "GET", 9 url: `@Url.Action("OnSetStat", "Home")` + "?StatType=" + strType + "&BillID=" + strBillID, 10 contenType: "application/json", 11 dataType: "json", 12 success: function (response) { 13 alert(response); 14 }, 15 failure: function () { 16 alert(`失敗,請重試一下或與系統維護員聯絡!`); 17 } 18 } 19 )
        }
20 }) 21 </script> 22 }

在controllers中

 1         public JsonResult OnSetStat(string StatType,string BillID)
 2         {
 3             string SqlStr = "";
 4             if (StatType == "Accept")
 5                 SqlStr = string.Format("UPDATE WorkList SET IsToBe=1,IsDelete=0,IsFinish=0 WHERE Id={0}", BillID);
 6 
 7     .....
 8 
 9             int returnEffectRow = 0;
10             using (SqlConnection currentCon=new SqlConnection(UtilConf.Configuration.GetConnectionString("DefaultConnection")))
11             {
12                 currentCon.Open();
13                 using (var currentComm = currentCon.CreateCommand())
14                 {
15                     currentComm.CommandText = SqlStr;
16                     returnEffectRow=currentComm.ExecuteNonQuery();
17                 }
18             }
19             return new JsonResult(returnEffectRow);
20         }        

 

相關文章