成功执行Ajax操作以打开另一个弹出窗口
本文关键字:另一个 窗口 执行 Ajax 操作 成功 | 更新日期: 2023-09-27 18:00:42
现在我正试图让一个Ajax窗口在完成时打开另一个窗口。基本上,第一个窗口询问用户是否想做某事,第二个窗口告诉他们成功了。第一个需要yes或no,第二个只需要ok。我如何告诉Ajax Actionlink在完成后打开另一个?我使用C#在MVC 3中工作。
现在我的ActionLink是:
@Ajax.ActionLink("Reset User Password", "ResetUserPW", "Admin",
new { userName = Model.UserName },
new AjaxOptions { Confirm = "Reset Password?", HttpMethod = "HttpGet" })
这很好(我知道没有OnSuccess,后面会提到这个事实)。它可以很好地执行逻辑并重置用户的密码。我就是不知道怎样才能打开另一扇窗户。这是我的控制器操作:
public ActionResult ResetUserPW(string userName)
{
string newExcept;
MembershipUser user = Membership.GetUser(userName);
if (user != null)
{
try
{
string newPassword = Membership.GeneratePassword(8, 2);
if (user.ChangePassword(user.GetPassword(), newPassword))
{
var mailMessage = new UserMailer();
return PartialView();
//return RedirectToAction("Users"); //Tried this return
result, no go
}
else
{
const string ErroExcept = "There was an error processing your request (the password reset has failed). Please try again.";
ModelState.AddModelError("", ErroExcept);
}
}
catch (Exception ex)
{
newExcept = String.Format("There was an error processing your request({0}). Please try again.", ex.Message);
ModelState.AddModelError("", newExcept);
}
}
else
{
newExcept = "There is no record of the specified user in the database.";
ModelState.AddModelError("", newExcept);
}
return RedirectToAction("Users");
}
由于执行正确,它永远不会到达最后一行代码。顶部没有数据标记,尽管我已经尝试将HttpMethod更改为POST方法并添加POST标记。此外,我非常清楚ActionLink中没有OnSuccess或OnCompletion。我试着在里面放了很多不同的东西,但没有结果,所以我修剪了它们。毕竟,整个问题是我在OnSuccess=区域中放了什么?
我对JQuery不是很好,也不太熟悉,我想这就是为什么我很难解决的原因。我已经进行了详尽的搜索,并编写了大量不同类型的JQuery代码,但无法将它们与Ajax链接一起使用。Controller操作接受userName,因为管理员执行此操作,并且需要为所有用户执行此操作(据说是为了停止不可避免的:"您可以使用user.Identity.name"语句获取用户的用户名)。另外,请告诉我修剪异常和ModelState代码是否会有所帮助。提前谢谢。
最终解决方案:
public ActionResult ResetUserPW(string userName)
{
string newExcept;
MembershipUser user = Membership.GetUser(userName);
if (user != null)
{
try
{
string newPassword = Membership.GeneratePassword(8, 2);
if (user.ChangePassword(user.GetPassword(), newPassword))
{
var mailMessage = new UserMailer();
mailMessage.AdminPWReset(user.UserName, newPassword, user.Email.SendAsync();
return Json(null);
}
else
{
ModelState.AddModelError("Password", "There was an error processing your request (the password reset has failed). Please try again");
}
}
catch (Exception ex)
{
ModelState.AddModelError("Password", String.Format("There was an error processing your request ({0}). Please try again.", ex.Message));
}
}
else
{
ModelState.AddModelError("Invalid User", "There is no record of the specified user in the database.");
}
if (!ModelState.IsValid)
{
return Json(GetModelStateErrors(ModelState));
}
return Json(null);
}
顶部没有动词。GetModelStateErrors定义如下:
private IEnumerable<ModelStateError> GetModelStateErrors(ModelStateDictionary dictionary)
{
foreach(var key in dictionary.Keys)
{
var error = dictionary[key].Errors.FirstOrDefault();
if(error != null)
yield return new ModelStateError(key, error.ErrorMessage);
}
}
最后,Ajax链接是:
@Ajax.ActionLink("Reset User Password", "ResetUserPW", "Admin",
new { userName = Model.UserName },
new AjaxOptions {
Confirm = "Reset Password?",
HttpMethod = "HttpGet",
OnSuccess="success"
})
Whoopsies,忘了添加我使用的最后一个Javascript。它和一克说的一样:
<script type="text/javascript">
function success(data) {
alert('You have successfully reset the user''s password!');
}
</script>
您可以在OnSuccess中放入一个javascript回调,如果AjaxLink调用返回非错误状态,该回调将运行,如下所示:
<script src="../../Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript">
function success(data) {
alert('Your password was reset');
}
</script>
@Ajax.ActionLink("Reset User Password", "ResetUserPW", "Admin",
new { userName = Model.UserName },
new AjaxOptions { Confirm = "Reset Password?", HttpMethod = "HttpPost", OnSuccess = "success" })
但是您需要一种返回验证错误的方法。所以我会尝试这样的东西:
[HttpPost]
public ActionResult ResetUserPW(string userName) {
string newExcept;
MembershipUser user = Membership.GetUser(userName);
if (user != null) {
try {
string newPassword = Membership.GeneratePassword(8, 2);
if (user.ChangePassword(user.GetPassword(), newPassword)) {
var mailMessage = new UserMailer();
}
else {
ModelState.AddModelError("Password", "There was an error processing your request (the password reset has failed). Please try again.");
}
}
catch (Exception ex) {
ModelState.AddModelError("Password", String.Format("There was an error processing your request({0}). Please try again.", ex.Message));
}
}
else {
ModelState.AddModelError("Password", "There is no record of the specified user in the database.");
}
if (!ModelState.IsValid)
return Json(GetModelStateStateErrors(ModelState));
return Json(null);
}
private IEnumerable<ModelStateError> GetModelStateStateErrors(ModelStateDictionary dictionary) {
foreach (var key in dictionary.Keys) {
var error = dictionary[key].Errors.FirstOrDefault();
if (error != null)
yield return new ModelStateError(key, error.ErrorMessage);
}
}
使用ModelState的简单DTO:
public class ModelStateError {
public string Property { get; set; }
public string Error { get; set; }
public ModelStateError(string key, string value) {
this.Property = key;
this.Error = value;
}
}
如果没有验证错误,success
中的data
参数将为null,否则,它将包含一组验证错误,您可以根据需要向用户显示这些错误。