c# - Why can't the value of ViewBag be cleared after PostBack? -


my controller name "demo". write 2 actions same name "index". first uses [httpget] , seconds [httppost].

but, when require postback view, value of viewbag.name in action [httpget] public actionresult index() {} can't cleared.

[httpget]         public actionresult index()         {             viewbag.name = "httpget";             return view();         }          [httppost]         public actionresult index(formcollection form)         {             viewbag.name = "httppost";             return view();         } 

in routeconfig.cs:

routes.maproute(                 name: "newroute",                 url: "demo/index/{type}",                 defaults: new { controller = "demo", action = "index", type = urlparameter.optional }             ); 

and view:

<form method="post" action="@url.action("index", "demo", new { type = @viewbag.name })">     <input type="submit" value="click me" /> </form>  @viewbag.name 

here problem: when click button, value of @viewbag.name in page "httppost". but, in url, it's /demo/index/httpget

why?

if navigate page request, you're executing method index(), , page rendered name httpget, create url form action /demo/index/httpget.

later, once press button, you're posting url created in previous step, since form posting you're executing index(formcollection form), , sets name httppost. url remains generated @ previous step.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -