我有一个存储过程如下:
ALTER PROCEDURE [dbo].[Addworkshop]
@title varchar(100),
@time varchar(10),
@date date,
@mandatory int,
@sid int
AS
BEGIN
SET NOCOUNT ON;
insert into Workshop(title, date, time, mandatory, sid)
values(@title, @date, @time, @mandatory, @sid)
declare @wid int
set @wid = scope_identity()
return @wid
END
我在ASP.NET MVC5 C#中的操作方法中调用它:
public JsonResult addWorkshop(Workshop workshop)
{
TLCWREntities context = new TLCWREntities();
var wid = db.Addworkshop(workshop.title, workshop.time, workshop.date, workshop.mandatory, workshop.sid);
return Json(wid);
}
使用JSON ajax调用此方法:
$.ajax({
type: 'POST',
url: '@Url.Action("addWorkshop")', // we are calling json method
dataType: 'json',
data: $.toDictionary(wsData),
success: function (wid) {
console.log("success add workshop wid: "+wid);
},
error: function (ex) {
console.log("err add workshop");
}
});
数据已成功插入到表中,当我在SQL Server Management Studio中执行存储过程时,它会返回正确的新插入行ID。然而,当我使用JSON ajax和操作方法调用存储过程时,它总是返回-1。
我正在使用Visual Studio 2013 MVC5 C#和SQL Server Studio 2008。
wid
的值是什么? - iuliu.net