当前,我有一个带有Post方法[form body]
的Web API控制器,该方法使用PostMan向数据库执行发布。
现在,我唯一要做的就是在提交HTML form='post'
并将其发布到数据库时,Web API中的post方法起作用。但是如何做到这一点呢?
答案 0 :(得分:0)
嘿,每个人都可以回答我自己的问题,只是为了将来的观众,这就是我所做的事情
带有ajax的HTML形式:
<form method="post" action="Api/Values/Complex" enctype="application/x-www-form-urlencoded">
<select id="thisSelect">
<option value="sp18br"> sp18br</option>
<option value="sp18cl"> sp18cl</option>
</select>
<br />
<br />
<input type="submit" value="submit" />
</form>
<script>
$("form").on("submit", function () {
var post = $.post("api/values/complex", { "": $("#thisSelect").val() });
return false;
});
</script>
ValuesController.cs:
[HttpPost]
[ActionName("Complex")]
public HttpResponseMessage Post([FromBody]string value)
{
if (value == "sp18br")
{
string cadena = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Users\\azib\\Desktop\\dfb; Extended Properties = dBASE IV; User ID = Admin; Password =";
string select = "Select * from sp18br ";
DataSet myDataSet = new DataSet();
OleDbConnection con = new OleDbConnection(cadena);
OleDbCommand myAccessCommand = new OleDbCommand(select, con);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
con.Open();
DataTable dt = new DataTable();
myDataAdapter.Fill(dt);
con.Close();
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con1 = new SqlConnection(cs))
{
con1.Open();
foreach (DataRow dr1 in dt.Rows)
{
SqlCommand cmd = new SqlCommand("sp_InsertLeafPickup", con1);
cmd.CommandType = CommandType.StoredProcedure;
//Insert stored procedure
cmd.Parameters.AddWithValue("@Address", dr1["PCOMBINED"]);
//cmd.Parameters.AddWithValue("@SeasonType", dr1["PSSSTREET"]);
cmd.Parameters.AddWithValue("@NextPickupdate", dr1["ZST"]);
cmd.ExecuteNonQuery();
}
con.Close();
con1.Close();
}
return Request.CreateResponse(HttpStatusCode.OK, "Ok");
}
if (value == "sp18cl")
{
string cadena1 = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\\Users\\azib\\Desktop\\dfb; Extended Properties = dBASE IV; User ID = Admin; Password =";
string select1 = "Select * from sp18cl ";
DataSet myDataSet1 = new DataSet();
OleDbConnection con2 = new OleDbConnection(cadena1);
OleDbCommand myAccessCommand1 = new OleDbCommand(select1, con2);
OleDbDataAdapter myDataAdapter1 = new OleDbDataAdapter(myAccessCommand1);
con2.Open();
DataTable dt1 = new DataTable();
myDataAdapter1.Fill(dt1);
con2.Close();
string cs1 = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con3 = new SqlConnection(cs1))
{
con3.Open();
foreach (DataRow dr1 in dt1.Rows)
{
SqlCommand cmd = new SqlCommand("sp_InsertLeafPickup", con3);
cmd.CommandType = CommandType.StoredProcedure;
//Insert stored procedure
cmd.Parameters.AddWithValue("@Address", dr1["PCOMBINED"]);
// cmd.Parameters.AddWithValue("@SeasonType", dr1["PSSSTREET"]);
cmd.Parameters.AddWithValue("@NextPickupdate", dr1["ZST"]);
cmd.ExecuteNonQuery();
}
con2.Close();
con3.Close();
}
}
return Request.CreateResponse(HttpStatusCode.OK, "Ok");
}