asp.net mvc 4 - Redirecting to an Action Method from another Action Method in the same controller -
i newbie in asp.net , doing web page application in mvc4 login functionality. index action method looks this-
public actionresult index() { var pagemodellist1 = new dataaccesslayer.dataaccess().getpageinfo(); viewdata["menulist"] = pagemodellist1.pagemodellist; return view(); }
and login action method looks like-
[httppost] public actionresult login(loginmodel model, string returnurl) { if (modelstate.isvalid) { var pagemodellist1 = new dataaccesslayer.dataaccess().getpageinfo(model.username,model.password); viewdata["menulist"] = pagemodellist1.pagemodellist; return redirecttoaction("index", "mycontroller"); } modelstate.addmodelerror("", "login failed"); return partialview("_login", model); }
what need is, when login successfully, redirecttoaction("index", "deimos") should take place 'menulist' there should new 'menulist' login action method. how it?
redirecttoaction
send 302 response browser new url location header value , browser make totally new request go page. new request has no idea did in previous request. viewdata
not work. may consider using tempdata
.
but tempdata's life until next request. after gone. if want on subsequent requests(like menu shown user), suggest read database table every time load page. can store items cache after first read avoid constant hit(s) database if worried that.
another option set menu items session variables , read there. not big fan of setting stuff session. prefer read cache (in data loaded db call) or so.
Comments
Post a Comment