我通过Ajax调用(我认为?)向我的控制器提交一种数据形式来处理。保存行后,我希望重定向到最初加载表单的控制器中的原始HttpGet操作。
我发现的是ajax调用工作,控制器操作触发,数据保存到数据库。但是,重新加载View后,屏幕永远不会刷新。
我的控制器中的动作'返回视图(模型)'上有一个断点,它会触发 - 但屏幕不会刷新。如果我使用firebug并查看html,我会看到新行应显示在我的视图中。但是,屏幕似乎根本没有重新加载。
我的Javascript:
<script type="text/javascript">
$(document).ready(function () {
$('.btnSubmitNewCard').click(function () {
var data = { cardNumber: $('.txtNewCardNumber').val(), cardHolder: $('.txtNewCardHolder').val(), expiryMonth: $('.txtNewExpiryMonth').val(), expiryYear: $('.txtNewExpiryYear').val(), active: $('.txtNewActive').val(), accountId: $('.Id').val() };
$.ajax({
url: '@Url.Action("SaveBankCard", "BankAccount")',
type: "POST",
contentType: "application/json",
data: JSON.stringify(data),
cache: false,
async: true,
success: function (result) {
console.log(result.toString());
if (result.Success == 'true') {
alert('Redirecting...');
window.location = '@Url.Action("EditBankAccount", "BankAccount", new {accountId = Model.Id})';
}
},
error: function () {
alert("Oh no");
}
});
});
});
</script>
上面的javascript调用的控制器方法(成功):
public ActionResult SaveBankCard(string cardNumber, string cardHolder, int expiryMonth, int expiryYear, string active, int accountId)
{
var card = new AccountCardDto
{
Id = 0,
AccountId = accountId,
Active = active == "on",
CardHolderName = cardHolder,
CardNumber = cardNumber,
ExpiryDate = new DateTime(2000 + expiryYear, expiryMonth, 1)
};
int id = new BankAccountService().SaveCard(card);
return RedirectToAction("EditBankAccount", new { bankAccountId = accountId });
}
然后从'RedirectToAction'调用调用的Controller Action:
[HttpGet]
[Authorize]
[OutputCache(Location = System.Web.UI.OutputCacheLocation.None)]
public ActionResult EditBankAccount(int? bankAccountId)
{
var model = new BankAccountModel();
if (bankAccountId != null)
{
....
}
return View(model);
}
最后一行,'返回视图(模型)'确实被调用。如果我检查'model',我会看到持久存储到数据库的新行。但是,正如我所说,屏幕不会刷新。
任何人都可以告诉我为什么,以及如何解决/改善我的情况?
答案 0 :(得分:1)
试试这个...你的方法SaveBankCard
在控制器和ajax中调用EditBankAccount
然后做一件事只在ajax或控制器中调用它
<script type="text/javascript">
$(document).ready(function () {
$('.btnSubmitNewCard').click(function () {
var data = { cardNumber: $('.txtNewCardNumber').val(), cardHolder: $('.txtNewCardHolder').val(), expiryMonth: $('.txtNewExpiryMonth').val(), expiryYear: $('.txtNewExpiryYear').val(), active: $('.txtNewActive').val(), accountId: $('.Id').val() };
$.ajax({
url: '@Url.Action("SaveBankCard", "BankAccount")',
type: "POST",
contentType: "application/json",
data: JSON.stringify(data),
cache: false,
async: true,
success: function (result) {
console.log(result.toString());
if (result != 0) **//if your method return int else check it null if your method return string**
{
alert('Redirecting...');
window.location = '@Url.Action("EditBankAccount", "BankAccount", new {bankAccountId= Model.Id})'; **//change name of parameters**
}
},
error: function () {
alert("Oh no");
}
});
});
});
</script>
控制器
public int SaveBankCard(string cardNumber, string cardHolder, int expiryMonth, int expiryYear, string active, int accountId)
{
var card = new AccountCardDto
{
Id = 0,
AccountId = accountId,
Active = active == "on",
CardHolderName = cardHolder,
CardNumber = cardNumber,
ExpiryDate = new DateTime(2000 + expiryYear, expiryMonth, 1)
};
int id = new BankAccountService().SaveCard(card);
int bankAccountId = accountId;
return bankAccountId ;
}