I know, I know, I know. I shouldn't be doing webforms inside of MVC, I completely agree. But, the people who sign my paycheck will not approve a complete conversion of our site to MVC right now. So I am taking incremental steps, page by page, to convert them over while adding new features in MVC.
So my question is how can I access the IsPostBack property from a controller?
Edit: To further clarify, I have a webform user control on my mvc master page which can initiate postbacks. I'm trying to identify these postbacks verses an mvc post. At this point I think I am going to just check the request form keys for a "__viewstate" key and if its found treat it as a postback.
A PostBack in ASP.NET Web Forms is an HTTP Post. IsPostBack is a standard pattern in Web Forms to determine if the page request is a GET or a POST. This pattern does not apply to MVC. In MVC, the initialization code goes in an [HttpGet] action and the form processing code goes in an [HttpPost] action.
There is no such thing as a stateful postback where things just automatically retain their state. You only have ViewModels that are bound to the view, and ViewModels that are posted by the view to the Controller. They don't even necessarily need to be of the same Type.
ASP.net postback is the default characteristic of the dot net server. It must require for communicating between client-side code and server-side code.
Hence to check whether the page was called using update panel or it was a complete postback we can use the below code. The ScriptManager. GetCurrent(this). IsInAsyncPostBack returns true when the server side code is invoked using a update panel.
In case anyone is still interested, you can test for a POST from inside an MVC Action Method like this:
if (Request.HttpMethod=="POST") {
}
There is no IsPostBack -- everything is either a POST or GET (or other HTTP verb). You can limit the HTTP verbs that your action allows, i.e., you'll never see a request from a disallowed verb, using the AcceptVerbsAttribute. For example, the following only allows POSTs.
[AcceptVerbs( HttpVerbs.Post )]
[ValidateAntiForgeryToken]
public ActionResult Update( int id )
{
}
If you need to have the same action name do both GET/POST and they actually do different things, you can either give them separate signatures or use the ActionNameAttribute to alias one of the actions so the methods can have different names.
[AcceptVerbs( HttpVerbs.Get)]
public ActionResult List()
{
}
[AcceptVerbs( HttpVerbs.Post )]
[ValidateAntiForgeryToken]
public ActionResult List( string filter, int page, int limit )
{
}
OR
[ActionName( "List" )]
[AcceptVerbs( HttpVerbs.Get)]
public ActionResult ListDisplay()
{
}
[AcceptVerbs( HttpVerbs.Post )]
[ValidateAntiForgeryToken]
public ActionResult List()
{
}
EDIT: Note that I've added the antiforgery token validation to the POST actions. You really should be using this to protect against cross-site scripting attacks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With