I call the same controller many times from _Layout.cshtml view. So in this controller, how can I discover at runtime if it's still same page that is rendering or if a brand new page request is being made?
In asp.net you can use ispostback to figure this out. How can you tell if a brand new request is being made for a page in MVC3?
Thanks
Page. IsPostBack property is use to check wheather page is post back.It return bool value.
IsPostBack event is generated by the web controls to alert the server to take respected action of the event generated. When the button is clicked then click event is generated which further cause ispostback event & it alerts the server to take respected action during postback event.
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.
There's no such think on MVC. You've actions that can handle POSTs, GETs or both. You can filter what each action can handle using [HttpPost]
and [HttpGet]
attributes.
On MVC, the closest you can get to IsPostBack
is something like this within your action:
public ActionResult Index()
{
if (Request.HttpMethod == "POST")
{
// Do something
}
return View();
}
Therefore,
[HttpPost]
public ActionResult Create(CreateModel model)
{
if (Request.HttpMethod == "POST") // <-- always true
{
// Do something
}
return RedirectToAction("Index");
}
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