开发者

Reject request and remain on referring page

I have a view which after a certain operation calls a new action in a different controller.

When this new action is loading 开发者_开发技巧its information, it may find out that the data is no longer valid (working against a 3rd party backend).

In this case I want to reject the request and remain on the calling page (i.e. not reload/call the referrers action method again).

How may this be accomplished?


you can write something in your controller action like

if(data not found)
{
    return Redirect(Request.UrlReferrer.AbsoluteUri);
}

if there is no url referral and you are typing url in browser's address bar it will throw null reference exception
Edit: in case of username/password validation we use one ActionResult for HttpGet and one of HttpPost request

[HttpGet]
public ActionResult Login()
{
    LoginViewModel model = new LoginViewModel();
    return View(model);//this will render the view for first time
}

[HttpPost]
public ActionResult Login(LoginViewModel model)
{
    if(!ValidUser(model))
        ModelState.AddModelError("form0", "user name or password incorrect");
    if(ModelState.IsValid)
    {
       return RedirectToAction("Home", "Index");//return to success page if correct
    }
    return View(model);//if we got here we have errors so render same view with errors 
}

in your view you should use

<%:Html.ValidationMessageFor(x=>x.UserName)%> with each field and
<%:Html.ValidationSummary()%> with your form
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜