C# ASP.NET MVC 4:对控制器进行Ajax调用以更新SQL Server数据库时出错
我正在尝试使用ASP.NETMVC4、jQuery和Ajax更新SQLServer中表的记录。但是,每当我单击“更新”时,都会出现以下错误: System.Data.SqlClient.SqlException未由用户代码处理C# ASP.NET MVC 4:对控制器进行Ajax调用以更新SQL Server数据库时出错,c#,sql-server,asp.net-mvc-4,error-handling,asp.net-ajax,C#,Sql Server,Asp.net Mvc 4,Error Handling,Asp.net Ajax,我正在尝试使用ASP.NETMVC4、jQuery和Ajax更新SQLServer中表的记录。但是,每当我单击“更新”时,都会出现以下错误: System.Data.SqlClient.SqlException未由用户代码处理 HResult=-2146232060 Message=参数化查询“(@ID int,@AlertStatus nvarchar(4000),@Comment nvarchar(4000))UPD”需要未提供的参数“@AlertStatus”。 Source=.Net S
HResult=-2146232060
Message=参数化查询“(@ID int,@AlertStatus nvarchar(4000),@Comment nvarchar(4000))UPD”需要未提供的参数“@AlertStatus”。
Source=.Net SqlClient数据提供程序 在进一步的研究中,我发现通过使用
?,可以避免这种情况??DBNull.Value
,但是我不想将列更新为NULL
。我怀疑错误的原因是在update函数中的Ajax调用中,数据没有发送到控制器(数据:{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");
}
})
});
控制器:
[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();
}
}
阶级
}对于通过分而治之解决这一问题应该非常简单 使用Chrome或其他浏览器,在设置任务的行上设置断点。注释并检查任务对象,查看AlertStatus在该点是否有值。然后切换到“网络”选项卡,然后继续单步执行代码。检查发送到控制器的请求的有效负载,并查看其中是否也包含AlertStatus。如果这些都是好的,那么问题就出在服务器端。如果不是,问题就出在你的客户身上 然后在开始时在控制器中设置断点,并在此处检查任务对象的AlertStatus。如果为null,则表示modelbinder出现问题。如果它有一个值,那么你在这一点上应该是好的,问题就在那之后的某个地方 此外,如果您不想允许该值为null,那么应该在尝试将其提交到数据库之前进行一些验证,并使用典型的MVC验证元素进行验证,然后将400个错误请求返回给客户端。验证的一部分可能在Task类中,我们目前无法看到它
祝你好运 这应该是通过分而治之解决的非常简单的问题 使用Chrome或其他浏览器,在设置任务的行上设置断点。注释并检查任务对象,查看AlertStatus在该点是否有值。然后切换到“网络”选项卡,然后继续单步执行代码。检查发送到控制器的请求的有效负载,并查看其中是否也包含AlertStatus。如果这些都是好的,那么问题就出在服务器端。如果不是,问题就出在你的客户身上 然后在开始时在控制器中设置断点,并在此处检查任务对象的AlertStatus。如果为null,则表示modelbinder出现问题。如果它有一个值,那么你在这一点上应该是好的,问题就在那之后的某个地方 此外,如果您不想允许该值为null,那么应该在尝试将其提交到数据库之前进行一些验证,并使用典型的MVC验证元素进行验证,然后将400个错误请求返回给客户端。验证的一部分可能在Task类中,我们目前无法看到它
祝你好运 我发现了问题,只是不得不改变:
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 = {};
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");
}
})
});
致:
更新不太可能出现问题,但您确实应该使用参数。添加而不是参数。添加WithValue
。您也可以共享任务类吗?通过读取错误,我认为变量“@AlertStatus”可能有问题。检查您的代码,确保您正确设置了它,并且它不是空的,因为可能就是这种情况。查找变量值的一个非常好的方法是在错误之前使用Console.Log(variable;)更新不太可能出现问题,但您确实应该使用参数。添加而不是参数。添加WithValue
。您也可以共享任务类吗?通过读取错误,我认为变量“@AlertStatus”可能有问题。检查您的代码,确保您正确设置了它,并且它不是空的,因为可能就是这种情况。查找变量值的一个非常好的方法是在错误之前使用Console.Log(variable;)
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);
});