如何检测我的页面是否是回发的结果

时间:2022-06-15 05:46:38

I have a combo box on an MVC3 application. When a new item is selected it does a post back as I want it to. All fine there. In the process I pop in a querystring and read it on page load. When the page refreshes it reads the query string and sets the other controls as needed.

我在MVC3应用程序上有一个组合框。当选择一个新项目时,它会根据我的要求发回一个帖子。一切都很好。在这个过程中,我弹出一个查询字符串并在页面加载时读取它。页面刷新时,它会读取查询字符串并根据需要设置其他控件。

However, I need to detect when the page reloads, that it is as a result of a postback, as opposed to the first time the page loads. This is because when the page loads initially, everything is messed up until someone picks something from the combobox.

但是,我需要检测页面何时重新加载,它是由于回发而不是第一次加载页面。这是因为当页面最初加载时,一切都搞砸了,直到有人从组合框中选择了一些内容。

However, a new user to the site wont know that and will just see a mess.

然而,该网站的新用户不会知道这一点,只会看到一团糟。

I understand that MVC3 apps don't have the same isPostback as ASP.Net has and for various reasons that I don't understand, I know it is somehow not considered "accaptable".

据我所知,MVC3应用程序与ASP.Net没有相同的isPostback,并且出于各种原因,我不明白,我知道它在某种程度上不被视为“适应”。

However, I am just interested in knowing if there is a 100% guaranteed reliable way to differentiate between a page first loading, and a postback in the same manner as was done in ASP.Net. If there is such a way, what is it and how can I implement it.

但是,我只是想知道是否有100%保证可靠的方法来区分页面首次加载和回发的方式与ASP.Net中的方式相同。如果有这样的方式,它是什么以及如何实现它。

I have seen other post's that do this:

我见过其他帖子这样做:

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

but I read other places that this is always true. So therefore if this is always true it will be true on first load too and in so being, I cant tell reliably if it is a postback or not. I know of course it is a postback of some sort. But it is not if it is a first load.

但我读到其他地方,这总是如此。因此,如果这始终是真的,那么在第一次加载时也是如此,因此,我无法可靠地判断它是否是回发。我当然知道这是某种回发。但它不是第一次加载。

Can anyone help me with an answer to this please. Also I am using the Razor view engine as opposed to standard aspx view engines.

任何人都可以帮我解决这个问题。此外,我使用Razor视图引擎而不是标准的aspx视图引擎。

3 个解决方案

#1


15  

In MVC, you typically have different actions for GET and POST:

在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
}

If you follow this pattern, there is no need to worry about a Page.IsPostBack-like property.

如果您遵循此模式,则无需担心类似Page.IsPostBack的属性。

#2


4  

As you said, there's no notion of Postback in ASP.NET MVC. What you cold do is to test the HTTP verb that was used to perform the request to see if it is POST. This could be either after a form submission or an AJAX request:

正如你所说,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
    }
    ...
}

#3


3  

You can decorate the action with the proper attributes

您可以使用适当的属性修饰操作

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

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

#1


15  

In MVC, you typically have different actions for GET and POST:

在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
}

If you follow this pattern, there is no need to worry about a Page.IsPostBack-like property.

如果您遵循此模式,则无需担心类似Page.IsPostBack的属性。

#2


4  

As you said, there's no notion of Postback in ASP.NET MVC. What you cold do is to test the HTTP verb that was used to perform the request to see if it is POST. This could be either after a form submission or an AJAX request:

正如你所说,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
    }
    ...
}

#3


3  

You can decorate the action with the proper attributes

您可以使用适当的属性修饰操作

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

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