[英]ASP.NET MVC - Insert into database after post method
我有兩個POST
方法, UpdateData
和UpdateTank
,它們從數據庫的存儲過程中拋出值。 我想將這些拋出的值添加到數據庫表中 - 這就是我嘗試使用POST
方法AddOrder
。
AddController
:
public class AddController : Controller
{
// GET: Add
public ActionResult Index(SlurryListOfClass SlurryListOfClass)
{
if (SlurryListOfClass.MesData.BatchNumber == null)
{
SlurryListOfClass.MesData.MatNumber = "";
///stuff
SlurryListOfClass.TankParam.PH = "";
}
return View(SlurryListOfClass);
}
[HttpPost]
public ActionResult UpdateData(string order)
{
SlurryListOfClass olc = new SlurryListOfClass();
//stuff
return View("Index", olc);
}
[HttpPost]
public ActionResult UpdateTank(string tank)
{
SlurryListOfClass slc = new SlurryListOfClass();
//stuff
return View("Index", slc);
}
//Post method to add order
[HttpPost]
public ActionResult AddOrder(SlurryListOfClass obj)
{
AddDetails(obj);
return View();
}
private SqlConnection con;
// To Handle connection related activities
private void connection()
{
string constr = ConfigurationManager.ConnectionStrings["SlurryOrderEntStoProc"].ToString();
con = new SqlConnection(constr);
}
// To add records into database
private void AddDetails(SlurryListOfClass obj)
{
connection();
string sql = "INSERT INTO ...";
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.AddWithValue("@OrderNr", obj.InsertTable.OrderNr ?? (object)DBNull.Value);
//...
com.Parameters.AddWithValue("@Comment", obj.InsertTable.Comment ?? (object)DBNull.Value);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
}
SlurryListOfClass
是我存儲所有其他模型的主要模型。
Index
視圖:
@model SlurryOrder2.Models.SlurryListOfClass
@using (Html.BeginForm("UpdateData", "Add", FormMethod.Post))
{
//Here I show two textboxes. The second one is the input parameter to the POST method UpdateTank.
@using (Html.BeginForm("UpdateTank", "Add", FormMethod.Post))
{
// Here I have 20-30 texboxes. The part is completed thanks to the UpdateTank POST method and the part
// is filled by the user. I want to pass these values into database.
}
<div class="panel-footer panel-custom">
<button type="button" class="btn btn-success btn-lg" id="addOrder">Add order</button>
</div>
<script>
$(document).ready(function () {
$("#addOrder").click(function () {
$.ajax(
{
type: "POST", //HTTP POST Method
url: "Add/Index", // Controller/View
data: { //Passing data
OrderNr: $("#txtOrder2").val(), //Reading text box values using Jquery
//...
Comment: $("#comment").val()
}
});
});
});
</script>
我嘗試了很多方法,但是在 POST 方法UpdateData
和UpdateTank
開始“工作”后,我無法將值添加到數據庫中。 例如,我嘗試使用RedirectToAction
將對象模型從 POST 方法UpdateTank
返回到 POST 方法AddOrder
。
首先,您不需要那些以前的表單或其相應的控制器操作。 ( UpdateData
和UpdateTank
)。 他們實際上並沒有做任何事情,這只是簡單地可以完成的大量開銷(當然,根據需要設置樣式和添加其他標記):
<input type="text" id="txtOrder2">
<input type="text" id="comment">
或者,如果您願意,可以使用<textarea>
代替。 當然,如果您綁定到Index
返回模型中的值,那么您仍然可以為此使用模型助手。 也許是這樣的:
@Html.TextBoxFor(m => m.OrderNr, new { id = "txtOrder2" })
很難說出您在Index
返回的內容與您嘗試發布到AddOrder
內容之間的真正關系。 但是,由於您使用 jQuery AJAX 手動發布,因此您可能不需要很多框架工具,只需使用您選擇的任何標記在頁面上顯示數據即可。
除此之外,在您的 AJAX 操作中,您發布到錯誤的控制器操作。 Index
不會向數據庫添加任何內容,而AddOrder
會。 只需更改網址:
url: "Add/AddOrder",
如果您不確定或者它是否可能相對於視圖發生變化,您還可以使用服務器端幫助程序代碼為您生成 URL:
url: "@Url.Action("AddOrder", "Add")"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.