asp.net mvc - By clicking submit button when form fields are not validated ReturnUrl from browser vanished -
by clicking submit button when form fields not validated returnurl browser vanished.
before clicking submit button browser seems like.
after clicking submit button(while form fields not validated till) browser seems like.
how maintain same browsing address after clicking submit button after login should redirect page.?
here action code
[httppost] [allowanonymous] [validateantiforgerytoken] public actionresult login(loginmodel model, string returnurl) { if (modelstate.isvalid) { if (membership.validateuser(model.username, model.password)) { formsauthenticationticket authticket = new formsauthenticationticket(1, model.username, datetime.now, datetime.now.addminutes(30), // value of time out property false, // value of ispersistent property string.empty, formsauthentication.formscookiepath); string encticket = formsauthentication.encrypt(authticket); httpcookie facookie = new httpcookie(formsauthentication.formscookiename, encticket); response.cookies.add(facookie); formsauthentication.setauthcookie(model.username, model.rememberme); return redirecttolocal(returnurl ?? url.action("index", "home")); } else { // if got far, failed, redisplay form modelstate.addmodelerror("", "the user name or password provided incorrect."); return view(); } } else { return view(model); } }
thanks in advance
assuming view has
@using (html.beginform(new { returnurl = viewbag.returnurl })) { ...
then when validation fails, need re-assign value in action method
if (modelstate.isvalid) { .... } else { // if got far, failed, redisplay form modelstate.addmodelerror("", "the user name or password provided incorrect."); viewbag.returnurl = returnurl; // add line return view(); }
Comments
Post a Comment