目前我有一个带有 Post 方法的 Web API 控制器,[form body]
它使用 PostMan 向数据库发布一个帖子。
现在我唯一想做的就是在form='post'
提交 Html 时让 Web API 中的 post 方法工作,然后让它发布到数据库。但是如何做到这一点呢?
目前我有一个带有 Post 方法的 Web API 控制器,[form body]
它使用 PostMan 向数据库发布一个帖子。
现在我唯一想做的就是在form='post'
提交 Html 时让 Web API 中的 post 方法工作,然后让它发布到数据库。但是如何做到这一点呢?
嘿大家我能够回答我自己的问题,只是为了未来的观众,这就是我所做的
带有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>
值控制器.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");
}