我有一个注册表单,注册成功后,它会重定向到另一个 View 。
我使用提交按钮将数据插入数据库,我想清除当前 View 中的值并将用户重定向到同一 Controller 下另一个 View 中的登录页面
。
提交类型没有执行 jquery 中提到的操作。请帮助我
这是我的代码预览
帐户/注册
帐户/登录
Accountcontroller.cs
[HttpPost]
public ActionResult Register(RegisterModel model, FormCollection form)
{
try {
---code
}
catch {
}
}
register.cshtml
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "accForm" }))
{
@html.textboxfor(.....)
@html.textboxfor(.....)
@html.textboxfor(.....)
@html.textboxfor(.....)
....
<input type="submit" value="CreateAccount" class="buttonclass" title="CreateAccount" ; onclick="SaveUser(this);"
}
function SaveUser(btnClicked) {
alert("clear");
document.getElementById("regname").value = '';
document.getElementById("regusername").value = '';
document.getElementById("regemail").value = '';
document.getElementById("regpassword").value = '';
document.getElementById("regpassword2").value = '';
$.ajax({
url: window.location.pathname + '/Account/Logon',
type: 'GET',
})
.success(function (result) {
alert("user saved successfully");
});
更新:
function SaveUser(btnClicked) {
document.getElementById("regname").value = '';
document.getElementById("regusername").value = '';
document.getElementById("regemail").value = '';
document.getElementById("regpassword").value = '';
document.getElementById("regpassword2").value = '';
alert('clear');
$.ajax({
type: "POST",
url: window.location.pathname + "Account/Register",
success: function (response) {
$.ajax({
url: window.location.pathname + '/Account/Logon',
type: 'GET',
})
.success(function (result) {
alert("user saved successfully");
// window.location ="";
});
},
error: function (xhr, status, error) {
alert("Failed");
}
});
}
我收到失败警报。 请帮忙
最佳答案
如果您想在成功登录时重定向页面,请在成功时编写重定向代码(ajax 成功)。 经验:
$.ajax({
url: window.location.pathname + '/Account/Logon',
type: 'GET',
})
.success(function (result) {
alert("user saved successfully");
// window.location ="";
});
关于asp.net-mvc - 在mvc中使用ajax调用重定向到另一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18097352/