1
我有一個帶有自動完成功能和一個按鈕的文本框。點擊按鈕模式彈出窗口打開.Modal彈出窗口有兩個文本框字段,即ProductName和Categoryame以及一個用於將此信息保存到數據庫的按鈕。 我已經成功地使用模態popup.But保存數據,但之後我得到了這個輸出{「status」:true,「message」:「成功保存。」}。如何在保存數據後使用Bootstrap模式彈出窗口在Asp.Net中保存數據後無需重新加載完整表單MVC
但我想返回與文本框和按鈕相同的初始視圖,以便保存的產品開始出現在自動完成文本框中。 我的控制器的代碼是:
[HttpPost]
public ActionResult Create(Product prod)
{
string message = "";
bool status = false;
if (ModelState.IsValid)
{
db.Products.Add(prod);
db.SaveChanges();
status = true;
message = "Successfully Saved.";
//string url = Url.Action("CreateProduct", "Products");
//return Json(new { success = true });
return new JsonResult { Data = new { status = status, message = message } };
}
return PartialView("Create", prod);
}
和我的jQuery代碼是:
function bindForm(dialog) {
$('form', dialog).submit(function() {
if ($("#formCrud").valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
dataType: 'json',
success: function (result) {
if (result.success) {
alert(result.message);
$('#myModal2').modal('hide');
} else {
$('#myModalContent2').html(result);
bindForm(dialog);
}
}
});
return false;
}
});
}
我更正了我的json對象,但現在我仍然將{「status」:true,「message」:「Success Saved。」,「success」:true}作爲輸出。 –
Thankyou Federico,你是對的,但我又犯了一個錯誤。我沒有調用bindForm()函數。 –