Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Page - IsPostback like functionality

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

like image 709
River Avatar asked Nov 26 '11 00:11

River


People also ask

How do we identify that the page is PostBack?

Page. IsPostBack property is use to check wheather page is post back.It return bool value.

How do I use IsPostBack?

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.

What is IsPostBack in ASP NET MVC?

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.

Is there PostBack in MVC?

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.


1 Answers

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");
}    
like image 98
Joao Avatar answered Oct 02 '22 20:10

Joao