【发布时间】:2020-02-05 12:40:17
【问题描述】:
我有两个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。
【问题讨论】:
-
这里的整体流程是非常奇怪和混乱的。您有表单发布到服务器,除了返回一个空对象之外什么都不做,为不同的 URL 返回相同的视图(这本身通常是有问题的)。然后在 AJAX 调用中,您将发布到
Index操作,它不会向数据库添加任何内容,只是再次返回相同的视图。看起来您不需要那些第一个表单及其相应的操作。看起来您只需要几个输入,然后将您的 AJAX 操作发布到AddOrder。还是我在这里遗漏了您的意图? -
我可能做了一些愚蠢的事情,因为这是我的第一个项目。我使用第一个表单根据存储过程中的参数填充 texbox。接下来,我想将这些填充的文本框添加到数据库中。
-
@KwiecMac 索引是 AddController 上的 HttpGet。但是您的 ajax 语句说,您正试图通过 POST 调用它。
-
@sam:由于
Index上没有明确的[HttpGet],那么它将仍然处理POST 请求。但在这段代码中,它确实似乎混淆了哪些请求应该去哪里以及哪些数据。 -
@sam 现在我看到了这个错误。感谢@David,我把它变成了:
url: "@Url.Action("AddOrder", "Add")"Thanks
标签: c# sql asp.net-mvc entity-framework