How to determine if my page is the result of a postback

I have a combo box in an MVC3 application. When a new item is selected, it makes the message back the way I want it. Everything is fine. In the process, I queue up and read it when the page loads. When the page refreshes, it reads the query string and installs other controls as needed.

However, I need to determine when the page reloads, what happens as a result of the postback, and not the first time the page loads. This is because when the page loads initially, everything is messed up until someone selects something from the drop-down list.

However, the new user on the site does not know this and will see a mess.

I understand that MVC3 applications do not have the same isPostback as ASP.Net, and for various reasons that I don’t understand, I know that this is somehow not considered “adaptable”.

However, I'm just curious to find out if there is a 100% guaranteed reliable way to differentiate between the first page load and postback in the same way as it was done in ASP.Net. If there is such a way, what is it and how to implement it.

I saw another entry that does this:

    public bool IsPostBack
    {
        get
        {
            return ViewBag.IsPostBack = (Request.HttpMethod == "POST");
        }
    }

but I read other places that this is always the case. Therefore, if this is always true, this will be true on first boot, and thus, I cannot say for sure whether this is a postback or not. I know, of course, that this is some kind of postback. But this is not the first download.

- , . Razor, aspx.

+5
3

MVC GET POST:

[HttpGet] // Actions are GET by default, so you can omit this
public ActionResult YourAction(int id)
{
    // GET
}

[HttpPost]
public ActionResult YourAction(YourModel model)
{
    // POST
}

, Page.IsPostBack -like.

+14

, ASP.NET MVC Postback. , , HTTP-, , , POST. , AJAX:

public ActionResult Foo()
{
    if (string.Equals("post", Request.HttpMethod, StringComparison.OrdinalIgnoreCase)
    {
        // The POST verb was used to invoke the controller action
    }
    ...
}
+4

You can decorate the action with the appropriate attributes.

[HttpGet]
public ActionResult Foo() { 
    //Do pre-postback stuff here 
}

[HttpPost]
public ActionResult Foo() { 
    //Do postback stuff here 
}
+3
source