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

时间:2016-06-09 17:26:12

标签: c# asp.net-mvc asp.net-mvc-5

在我的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);为他们创建一些东西。但之后我希望重新加载页面并显示一些新的内容,例如“成功!我们提交了您的请求”,我稍后会在那里添加一个OK按钮,但目前是这样的:

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

但这不起作用,在提交后,它会重新加载带有“GET”请求的页面,从而再次显示该表单。我希望他们现在看到页面的另一部分是成功消息。

1 个答案:

答案 0 :(得分:1)

  

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

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