使用 ASP.NET 模型和 AJAX POST 的存储过程的 Web API 问题
Web API issue with stored procedure using ASP.NET Model and AJAX POST
我 运行 遇到参数为空或未提供的问题。以下错误如下:
Procedure or function 'Sp_Warning_Add' expects parameter '@Full_Name', which was not supplied.
我的 ApiController:
Database_Access_Layer.Db dblayer = new Database_Access_Layer.Db();
[HttpPost]
public IHttpActionResult AddWarning([FromBody]WarningData Warning)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
dblayer.Add_Warning(Warning);
return Ok(Warning.licenseNumber);
}
catch (Exception e)
{
return Ok(e);
}
}
我的模型结构:
public class WarningData
{
public string fullName { get; set; }
public string licenseNumber { get; set; }
public string warningDateIssued { get; set; }
public string warningLocation { get; set; }
public string warningOfficer { get; set; }
public string warningDetails { get; set; }
}
我的数据库访问层:
public class Db
{
// Add Warning Procedure
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
WarningData Warning = new WarningData();
public void Add_Warning(WarningData Warning)
{
SqlCommand com = new SqlCommand("Sp_Warning_Add", con)
{
CommandType = CommandType.StoredProcedure
};
com.Parameters.AddWithValue("@Full_Name", Encoder.HtmlEncode(Warning.fullName));
com.Parameters.AddWithValue("@Date", Encoder.HtmlEncode(Warning.warningDateIssued));
com.Parameters.AddWithValue("@Ticket_Details", Encoder.HtmlEncode(Warning.warningDetails));
com.Parameters.AddWithValue("@Ticket_Unit", Encoder.HtmlEncode(Warning.warningOfficer));
com.Parameters.AddWithValue("@Ticket_Location", Encoder.HtmlEncode(Warning.warningLocation));
com.Parameters.AddWithValue("@Ticket_Fine", "0");
com.Parameters.AddWithValue("@Ticket_Jailtime", "0");
com.Parameters.AddWithValue("@Ticket_Warrant_Active", "No");
com.Parameters.AddWithValue("@Ticket_Charges", "Warning");
com.Parameters.AddWithValue("@License_Number", Encoder.HtmlEncode(Warning.licenseNumber));
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
我的存储过程:
CREATE proc Sp_Warning_Add
@Full_Name NVARCHAR(MAX),
@Date NVARCHAR(MAX),
@Ticket_Details NVARCHAR(MAX),
@Ticket_Unit NVARCHAR(MAX),
@Ticket_Location NVARCHAR(MAX),
@Ticket_Fine NVARCHAR(MAX),
@Ticket_Jailtime NVARCHAR(MAX),
@Ticket_Warrant_Active NVARCHAR(MAX),
@Ticket_Charges NVARCHAR(MAX),
@License_Number NVARCHAR(MAX)
AS
BEGIN
INSERT INTO tickets
VALUES (@Full_Name, @Date, @Ticket_Details, @Ticket_Unit, @Ticket_Location,
@Ticket_Fine, @Ticket_Jailtime, @Ticket_Warrant_Active,
@Ticket_Charges, @License_Number)
END
我的AJAX电话:
$('#createWarningBtn').click(function (event) {
var warningFormSerialized = jQFormSerializeArrToJson($("#warningForm").serializeArray());
$.ajax({
type: "POST",
url: "/api/Civilian/AddWarning",
data: JSON.stringify({ Warning: warningFormSerialized }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data) {
},
failure: function (data) {
alert("Message: " + r.Message);
alert("StackTrace: " + r.StackTrace);
alert("ExceptionType: " + r.ExceptionType);
},
complete: function (data) {
civTickets(data.responseJSON.d);
$('#warningForm').dialog('close');
console.log(data)
}
});
event.preventDefault();
});
值不存在到底是我做错了什么? 运行 这个 AJAX 对 WebMethod 的调用有效,但是当我以这种方式使用 APIController 时,它不起作用。谢谢您的帮助。
我明白了。它在 AJAX 通话中。
data: JSON.stringify({ Warning: warningFormSerialized }),
应该是:
data: JSON.stringify(warningFormSerialized),
我 运行 遇到参数为空或未提供的问题。以下错误如下:
Procedure or function 'Sp_Warning_Add' expects parameter '@Full_Name', which was not supplied.
我的 ApiController:
Database_Access_Layer.Db dblayer = new Database_Access_Layer.Db();
[HttpPost]
public IHttpActionResult AddWarning([FromBody]WarningData Warning)
{
try
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
dblayer.Add_Warning(Warning);
return Ok(Warning.licenseNumber);
}
catch (Exception e)
{
return Ok(e);
}
}
我的模型结构:
public class WarningData
{
public string fullName { get; set; }
public string licenseNumber { get; set; }
public string warningDateIssued { get; set; }
public string warningLocation { get; set; }
public string warningOfficer { get; set; }
public string warningDetails { get; set; }
}
我的数据库访问层:
public class Db
{
// Add Warning Procedure
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
WarningData Warning = new WarningData();
public void Add_Warning(WarningData Warning)
{
SqlCommand com = new SqlCommand("Sp_Warning_Add", con)
{
CommandType = CommandType.StoredProcedure
};
com.Parameters.AddWithValue("@Full_Name", Encoder.HtmlEncode(Warning.fullName));
com.Parameters.AddWithValue("@Date", Encoder.HtmlEncode(Warning.warningDateIssued));
com.Parameters.AddWithValue("@Ticket_Details", Encoder.HtmlEncode(Warning.warningDetails));
com.Parameters.AddWithValue("@Ticket_Unit", Encoder.HtmlEncode(Warning.warningOfficer));
com.Parameters.AddWithValue("@Ticket_Location", Encoder.HtmlEncode(Warning.warningLocation));
com.Parameters.AddWithValue("@Ticket_Fine", "0");
com.Parameters.AddWithValue("@Ticket_Jailtime", "0");
com.Parameters.AddWithValue("@Ticket_Warrant_Active", "No");
com.Parameters.AddWithValue("@Ticket_Charges", "Warning");
com.Parameters.AddWithValue("@License_Number", Encoder.HtmlEncode(Warning.licenseNumber));
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
我的存储过程:
CREATE proc Sp_Warning_Add
@Full_Name NVARCHAR(MAX),
@Date NVARCHAR(MAX),
@Ticket_Details NVARCHAR(MAX),
@Ticket_Unit NVARCHAR(MAX),
@Ticket_Location NVARCHAR(MAX),
@Ticket_Fine NVARCHAR(MAX),
@Ticket_Jailtime NVARCHAR(MAX),
@Ticket_Warrant_Active NVARCHAR(MAX),
@Ticket_Charges NVARCHAR(MAX),
@License_Number NVARCHAR(MAX)
AS
BEGIN
INSERT INTO tickets
VALUES (@Full_Name, @Date, @Ticket_Details, @Ticket_Unit, @Ticket_Location,
@Ticket_Fine, @Ticket_Jailtime, @Ticket_Warrant_Active,
@Ticket_Charges, @License_Number)
END
我的AJAX电话:
$('#createWarningBtn').click(function (event) {
var warningFormSerialized = jQFormSerializeArrToJson($("#warningForm").serializeArray());
$.ajax({
type: "POST",
url: "/api/Civilian/AddWarning",
data: JSON.stringify({ Warning: warningFormSerialized }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data) {
},
failure: function (data) {
alert("Message: " + r.Message);
alert("StackTrace: " + r.StackTrace);
alert("ExceptionType: " + r.ExceptionType);
},
complete: function (data) {
civTickets(data.responseJSON.d);
$('#warningForm').dialog('close');
console.log(data)
}
});
event.preventDefault();
});
值不存在到底是我做错了什么? 运行 这个 AJAX 对 WebMethod 的调用有效,但是当我以这种方式使用 APIController 时,它不起作用。谢谢您的帮助。
我明白了。它在 AJAX 通话中。
data: JSON.stringify({ Warning: warningFormSerialized }),
应该是:
data: JSON.stringify(warningFormSerialized),