重新加载页面并在页面中显示新视图



在我的AccountController类中,我有以下内容:

public ActionResult ForgotPassword(ForgotPasswordViewModel fpModel = null)
{
string method = HttpContext.Request.HttpMethod;
if (method == "GET")
{
ViewBag.Status = "CREATE_TASK";
ForgotPasswordViewModel model = this.ForgotPasswordManager.LoadForgotPasswordSettings();
if (model != null && !string.IsNullOrWhiteSpace(model.ForgotPasswordMethod) && model.ForgotPasswordMethod.Trim().ToUpper() == "TASKS")
return View(model);
}
if (method == "POST")
{
ViewBag.Status = "TASK_CREATED";
this.CreateTask(fpModel);
}
return RedirectToAction("ForgotPassword"); // Prob this is wrong? 
}

然后在我看来,我有这个:

@model Models.Account.ForgotPasswordViewModel
<div class="container" style="background-color: #f6f6f6">
@if (ViewBag.Status == "CREATE_TASK")
{
<div class="form-group">
<h4 id="SearchWelcomeHeader">Password Request</h4>
</div>
using (Html.BeginForm("ForgotPassword", "Account", FormMethod.Post, new { role = "form", @class = "form-horizontal" }))
{
// some textboxes to fill up a password request form go in here
<button id="submit" class="btn btn-primary" style="width: 20%">Submit</button>
<button id="cancel" class="btn btn-secondary" style="width: 20%">Cancel</button>
}
}
@if (ViewBag.Status == "TASK_CREATED")
{
<p> Good Job ! You requested a new password! </p>
}
</div>      

因此,我试图实现的是:首先,他们进入该页面,填写他们的密码请求表并提交,所以现在是POST。所以在POST中,我使用代码中的this.CreateTask(fpModel);在DB中为他们创建一些东西。但在那之后,我希望页面被重新加载,并显示一些新的东西,比如"成功!我们提交了你的请求",我稍后也会添加一个OK按钮,但目前是这样的:

@if (ViewBag.Status == "TASK_CREATED")
{
<p> Good Job ! You requested a new password! </p>
ALSO A BUTTON, Will Add Later 
}

但这不起作用,在他们提交后,它会用"GET"请求重新加载页面,从而再次显示表单。我想让他们现在看到页面上成功信息的另一部分。

RedirectToAction方法向浏览器返回HTTP 302响应,这会导致浏览器向指定的操作发出GET请求

因此,将return RedirectToAction("ForgotPassword");修改为return View(fpModel);

最新更新