asp.net mvc - How can ViewBag data be saved after a form post? -
so have viewbag.something
, data randomly generated. in view, set label @html.labelfor(m => m.something, (string)viewbag.something)
. works out when submit form there errors , if there errors, need label remain same, don't want dynamic/random data anymore wouldn't call controller method generated viewbag. there way retain value without having private variable in controller? nice way mvc/razor it?
option 1:
pass value of "viewbag.something" controller using route values:
@html.actionlink("buttontext", "actionname", new { = @viewbag.something })
option 2: can use tempdata.
public actionresult index() { var = "yourvalue"; tempdata["something"] = something; ....... } public actionresult otheraction() { var = tempdata["something "]; ........... }
passing state between action methods
action methods might have pass data action, such if error occurs when form being posted, or if method must redirect additional methods, might occur when user directed login view , original action method.
an action method can store data in controller's tempdatadictionary object before calls controller's redirecttoaction method invoke next action. tempdata property value stored in session state. action method called after tempdatadictionary value set can values object , process or display them. value of tempdata persists until read or until session times out. persisting tempdata in way enables scenarios such redirection, because values in tempdata available beyond single request.
Comments
Post a Comment