[英]ASP.NET MVC 4: error when making Ajax call to controller to update SQL Server database
我正在嘗試使用 ASP.NET MVC 4、jQuery 和 Z3EB7106C3417DAC506 但是,每當我單擊更新時,我都會收到以下錯誤:
System.Data.SqlClient.SqlException 未被用戶代碼處理
H結果=-2146232060
消息=參數化查詢 '(@ID int,@AlertStatus nvarchar(4000),@Comment nvarchar(4000))UPD' 需要參數 '@AlertStatus',但未提供該參數。
Source=.Net SqlClient 數據提供者
在進一步的研究中,我發現這可以通過使用?? DBNull.Value
來避免。 ?? DBNull.Value
,但是我不想將該列更新為NULL
。 I suspect the reason for the error is in my Ajax call in the update function where the data is not being sent to the controller (data: '{task:' + JSON.stringify(task) + '}',).
請參閱下面的代碼,感謝您的幫助。
jQuery Ajax
$("body").on("click", "#tblTask .Edit", function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find("input").length > 0) {
$(this).find("input").show();
$(this).find("span").hide();
}
});
row.find(".Update").show();
row.find(".Cancel").show();
row.find(".Delete").hide();
$(this).hide();
});
$("body").on("click", "#tblTask .Update", function () {
var row = $(this).closest("tr");
$("td", row).each(function () {
if ($(this).find("input").length > 0) {
var span = $(this).find("span");
var input = $(this).find("input");
span.html(input.val());
span.show();
input.hide();
}
});
row.find(".Edit").show();
row.find(".Delete").hide();
row.find(".Cancel").hide();
$(this).hide();
var task = {};
task.taskID = row.find(".taskID").find("span").html();
task.GroupSubsidiary = row.find(".GroupSub").find("span").html();
task.FunctionName = row.find(".Fname").find("span").html();
task.FunctionDesc = row.find(".Fdesc").find("span").html();
task.CheckPeriod = row.find(".Checkp").find("span").html();
task.Profiledate = row.find(".Pdate").find("span").html();
task.PeriodDay = row.find(".Pday").find("span").html();
task.AlertStatus = row.find(".Status").find("span").html();
task.Comment = row.find(".Comment").find("span").html();
$.ajax({
type: 'POST',
url: "@Url.Action("UpdateTask","Task")",
data: '{task:' + JSON.stringify(task) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Updated Sucessfully");
},
error: function () {
alert("An Error Occured");
}
})
});
Controller:
[HttpPost]
public ActionResult UpdateTask(Task tasks)
{
string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
string query = "UPDATE AS_AlertsDefinition SET GroupSubsidiary= @GroupSubsidiary, FunctionName= @FunctionName, FunctionDesc= @FunctionDesc, CheckPeriod= @CheckPeriod, Profiledate= @Profiledate, PeriodDay= @PeriodDay, AlertStatus= @AlertStatus, Comment= @Comment WHERE ID= @ID";
using (SqlConnection sqlcon = new SqlConnection(constr))
{
using (SqlCommand sqlcmd = new SqlCommand(query))
{
sqlcmd.Parameters.AddWithValue("@ID", tasks.ID);
sqlcmd.Parameters.AddWithValue("@GroupSubsidiary", tasks.GroupSubsidiary);
sqlcmd.Parameters.AddWithValue("@FunctionName", tasks.FunctionName);
sqlcmd.Parameters.AddWithValue("@FunctionDesc", tasks.FunctionDesc);
sqlcmd.Parameters.AddWithValue("@CheckPeriod", tasks.CheckPeriod);
sqlcmd.Parameters.AddWithValue("@Profiledate", tasks.Profiledate);
sqlcmd.Parameters.AddWithValue("@PeriodDay", tasks.PeriodDay);
sqlcmd.Parameters.AddWithValue("@AlertStatus", tasks.AlertStatus);
sqlcmd.Parameters.AddWithValue("@Comment", tasks.Comment);
sqlcmd.Connection = sqlcon;
sqlcon.Open();
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
}
}
return new EmptyResult();
}
}
Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AlertNotificationWeb.Models
{
public class Task
{
public int ID { get; set; }
public string GroupSubsidiary { get; set; }
public string FunctionName { get; set; }
public string FunctionDesc { get; set; }
public string CheckPeriod { get; set; }
public string Profiledate { get; set; }
public int PeriodDay { get; set; }
public string AlertStatus { get; set; }
public string Comment { get; set; }
}
}
這應該很容易通過分而治之來解決。
使用 Chrome 或任何其他瀏覽器,在設置任務的行上設置斷點。注釋並檢查任務 object 以查看 AlertStatus 在該點是否有值。 然后切換到網絡選項卡,然后繼續單步執行代碼。 檢查發送到您的 controller 的請求的有效負載,看看它是否也包含 AlertStatus。 如果一切都很好,那么問題出在服務器端。 如果不是,問題出在您的客戶端。
然后在開始時在 controller 中設置斷點,並在那里檢查任務 object 的 AlertStatus。 如果它的 null,那么 modelbinder 出了問題。 如果它有一個價值,那么你應該在那一點上做得很好,而問題就在那之后的某個地方。
此外,如果您不想允許 null 用於該值,則應在嘗試將其提交到數據庫之前進行一些驗證,並使用典型的 MVC 驗證元素來執行此操作並將 400 錯誤請求返回給客戶端。 該驗證的一部分可能在任務 class 中,我們目前看不到。
祝你好運!
我發現了問題,只需要更改:
var task = {};
task.taskID = row.find(".taskID").find("span").html();
task.GroupSubsidiary = row.find(".GroupSub").find("span").html();
task.FunctionName = row.find(".Fname").find("span").html();
task.FunctionDesc = row.find(".Fdesc").find("span").html();
task.CheckPeriod = row.find(".Checkp").find("span").html();
task.Profiledate = row.find(".Pdate").find("span").html();
task.PeriodDay = row.find(".Pday").find("span").html();
task.AlertStatus = row.find(".Status").find("span").html();
task.Comment = row.find(".Comment").find("span").html();
$.ajax({
type: 'POST',
url: "@Url.Action("UpdateTask","Task")",
data: '{task:' + JSON.stringify(task) + '}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Updated Sucessfully");
},
error: function () {
alert("An Error Occured");
}
})
});
至:
var task = JSON.stringify({
taskID: row.find(".taskID").find("span").html(),
GroupSubsidiary: row.find(".GroupSub").find("span").html(),
FunctionName: row.find(".Fname").find("span").html(),
FunctionDesc: row.find(".Fdesc").find("span").html(),
CheckPeriod: row.find(".Checkp").find("span").html(),
Profiledate: row.find(".Pdate").find("span").html(),
PeriodDay: row.find(".Pday").find("span").html(),
AlertStatus: row.find(".Status").find("input").val(),
Comment: row.find(".Comment").find("span").html(),
});
$.ajax({
type: 'POST',
url: "@Url.Action("UpdateTask","Task")",
data: task,
contentType: "application/json",
dataType: "json",
success: function () {
alert("Updated Sucessfully");
},
error: function () {
alert("An Error Occured");
}
});
console.log(task);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.