c# - Multiple TempData variables passed to same action -
i'm creating asp.net web application using mvc , entity framework. have 2 different success messages passing index action when user clocks in or clocks out. success message print correctly when user clocks in, not when user clocks out reason.
the actions similar , used of same conventions can't figure out why 1 print , not. i've tried debugging , there no red flags , updates in database should. not possible pass multiple tempdata variables same action?
here relevant code:
controller
// get: timeclocks public actionresult index() { viewbag.clockinsuccess = tempdata["clockinsuccess"]; viewbag.clockoutsuccess = tempdata["clockoutsuccess"]; return view(); } [httppost] public actionresult clockin(timeclock timeclock) { if(db.timeclocks.tolist().count == 1) { modelstate.addmodelerror("existserror", "you clocked in at" + timeclock.clockin); } string currentuserid = user.identity.getuserid(); applicationuser currentuser = db.users.firstordefault(x => x.id == currentuserid); timeclock.applicationuser = currentuser; timeclock.clockin = datetime.now; if (modelstate.isvalid) { db.timeclocks.removerange(db.timeclocks.tolist()); db.timeclocks.add(timeclock); db.savechanges(); tempdata["clockinsuccess"] = "you clocked in @ " + timeclock.clockin; return redirecttoaction("index"); } return redirecttoaction("index", timeclock); } [httppost] public actionresult clockout(timeclock timeclock) { timeclock = db.timeclocks.firstordefault(); if(timeclock.clockin == null) { modelstate.addmodelerror("nullerror", "you must clock in before can clock out."); return redirecttoaction("index"); } timeclock.clockout = datetime.now; if (modelstate.isvalid) { db.entry(timeclock).state = entitystate.modified; db.savechanges(); tempdata["clockoutsuccess"] = "you clocked out @ " + timeclock.clockout; return redirecttoaction("index"); } return redirecttoaction("index", timeclock); }``
view
@model finalproject.models.timeclock @{ viewbag.title = "create"; } <h2>employee time clock</h2> @using (html.beginform("clockin", "timeclocks")) { @html.antiforgerytoken() <div class="form-horizontal"> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="clock in" class="btn btn-lg" /> </div> </div> </div> } @using (html.beginform("clockout", "timeclocks")) { @html.antiforgerytoken() <div class="form-horizontal"> @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="clock out" class="btn btn-lg" /> </div> </div> </div> } @{ if (@viewbag.clockinsuccess != "") { <p class="alert-success">@viewbag.clockinsuccess</p> } else if (@viewbag.clockoutsuccess != "") { <p class="alert-success">@viewbag.clockoutsuccess</p> } } <div> @html.actionlink("back list", "index") </div>
tempdata lost once read need peek or keep:
viewbag.clockinsuccess = tempdata.peek("clockinsuccess"); viewbag.clockoutsuccess = tempdata.peek("clockoutsuccess");
http://www.codeproject.com/articles/818493/mvc-tempdata-peek-and-keep-confusion
Comments
Post a Comment