开发者

How to implement a page contains a form, and after post the user can refresh without asking to repost?

I am making a website and I want the user to register and validate fields before sending to the data access layer.

The page contains 2 forms, login and register forms.

When the user press on register button the url of the page become /uers/register but if the user refreshed the p开发者_运维问答age the browser ask him if he want to repost or not.

I want to implement it as if the user refreshed the page it will not ask for repost.

To make things more clear by example: go to stackoverflow.com login page and press Login with empty openid, it will show error message but when refresh the page it will not ask for repost.

How stackoverflow doing this while the form is post not get?


This is called the redirect after post pattern and in ASP.NET MVC it could look like this:

public ActionResult Index()
{
    var model = new SomeViewModel();
    return View(model);
}

[HttpPost]
public ActionResult Index(SomeViewModel model)
{
    if (!ModelState.IsValid)
    {
        // validation failed => redisplay the form
        return View(model);
    }
    // Validation passed => update database and redirect
    return RedirectToAction("Index");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜