c# - A straightforward answer to using session variables in MVC Views? -
i having difficulties utilizing session variables in mvc views. in example below, if condition in view not met when set isloggedin session variable true via controller. note: trimmed of irrelevant code easier reading. in advance!
controller:
public actionresult index(int id = 0) { jobsummarymodelhelper jobdetails = new jobsummarymodelhelper(); jobdetails.id = id; jobdetails.jdata = ..... return view(jobdetails); } public actionresult authenticate() { ..... int usercount = db.jobboardusers.where(u => u.userid.equals(un) && u.passcode.equals(pc)).select(u => new accountmodel() { uid = u.id }).count(); if (usercount > 0) { httpcontext.session["issignedin"].equals(true); } return redirecttoaction("index", jobdetails);
view:
..... @if (convert.toboolean(session["issignedin"])) { <fieldset> <legend></legend> <p style="color: red; font-weight: bold;">logged in! </p> </fieldset> }
your controller's action not setting session variable true.
to need replace
httpcontext.session["issignedin"].equals(true);
with
httpcontext.session["issignedin"] = true;
the equals method not change value, method tells if 2 things equal.. has return value, you're doing nothing it
Comments
Post a Comment