简体   繁体   English

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

[英]How do I detect if my page is the result of a postback

I have a combo box on an MVC3 application. 我在MVC3应用程序上有一个组合框。 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. 页面刷新时,它会读取查询字符串并根据需要设置其他控件。

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. 但是,我只是想知道是否有100%保证可靠的方法来区分页面首次加载和回发与ASP.Net中的方式相同。 If there is such a way, what is it and how can I implement it. 如果有这样的方式,它是什么,我该如何实现它。

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视图引擎。

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的属性。

As you said, there's no notion of Postback in ASP.NET MVC. 正如你所说,ASP.NET MVC中没有Postback的概念。 What you cold do is to test the HTTP verb that was used to perform the request to see if it is POST. 你冷酷的做法是测试用于执行请求的HTTP动词,看它是否是POST。 This could be either after a form submission or an AJAX request: 这可以是在表单提交或AJAX请求之后:

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

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 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM