【发布时间】:2019-01-24 20:59:53
【问题描述】:
所以这对我来说很难解释。我有一个剃须刀页面,当单击一个按钮时,它会调用一个 javascript 函数,该函数会对后端的处理程序进行 ajax 调用。处理程序做一些事情并获得一个我想传递给另一个页面的 id。我正在尝试在后端使用 RedirectToPage 功能,但屏幕永远不会打开。它成功调用了处理程序,但是当处理程序返回时,什么也没有发生。有没有办法做到这一点?
这是从被点击的按钮调用的 javascript/ajax 代码。
@section scripts{
<script>
// Get the account ID Data from the row selected and return that to the program.
function getIDData(el) {
var ID = $(el).closest('tr').children('td:first').text();
var iddata = {
'ID': ID
}
console.log(iddata);
return iddata;
}
// Submit the data to a function in the .cs portion of this razor page.
$('.copybtn').click(function () {
var accountid = JSON.stringify(getIDData(this));
$.ajax({
url: '/Copy_Old_Account?handler=CopyData',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
type: 'POST',
dataType: 'json',
data: { offenderid: offenderid },
success: function (result) {
},
});
});
</script>
}
对于我从 ajax 调用中调用的代码背后的代码,如下:
public ActionResult OnPostCopyData (string accountid)
{
// Do my other stuff here
return RedirectToPage("Account_Information", new { id = account.Account_ID });
}
任何帮助将不胜感激,如果没有意义,我可以尝试解决任何问题。
【问题讨论】:
-
有没有尝试返回id,然后从ajax请求重定向到成功状态?
-
@joseatchang 我没有尝试过,我不确定该怎么做。你有什么我可以看的例子吗?
-
查看第一个答案,他指的是对象传递给
ajax方法,一旦获得对请求的肯定响应就会触发成功。 -
普通的 html 表单可能比 ajax 更好,因为您不会尝试在不重新加载页面的情况下发送/检索数据。
标签: javascript ajax asp.net-mvc razor-pages