ASP.NET MVC - Is IsPostBack still here?

前端 未结 11 2077
我在风中等你
我在风中等你 2020-12-09 17:09

I know, I know, I know. I shouldn\'t be doing webforms inside of MVC, I completely agree. But, the people who sign my paycheck will not approve a complete conversion of ou

相关标签:
11条回答
  • 2020-12-09 17:33

    There is no IsPostBack -- everything is either a POST or GET (or other HTTP verb). You can limit the HTTP verbs that your action allows, i.e., you'll never see a request from a disallowed verb, using the AcceptVerbsAttribute. For example, the following only allows POSTs.

      [AcceptVerbs( HttpVerbs.Post )]
      [ValidateAntiForgeryToken]
      public ActionResult Update( int id )
      {
      }
    

    If you need to have the same action name do both GET/POST and they actually do different things, you can either give them separate signatures or use the ActionNameAttribute to alias one of the actions so the methods can have different names.

      [AcceptVerbs( HttpVerbs.Get)]
      public ActionResult List()
      {
      }
    
      [AcceptVerbs( HttpVerbs.Post )]
      [ValidateAntiForgeryToken]
      public ActionResult List( string filter, int page, int limit )
      {
      }
    

    OR

      [ActionName( "List" )]
      [AcceptVerbs( HttpVerbs.Get)]
      public ActionResult ListDisplay()
      {
      }
    
      [AcceptVerbs( HttpVerbs.Post )]
      [ValidateAntiForgeryToken]
      public ActionResult List()
      {
      }
    

    EDIT: Note that I've added the antiforgery token validation to the POST actions. You really should be using this to protect against cross-site scripting attacks.

    0 讨论(0)
  • 2020-12-09 17:35

    You can use this piece of code in Razor

    @if(IsPost)
    {
    //dosomething
    }
    else
    {
    //do some other thing
    }
    
    0 讨论(0)
  • 2020-12-09 17:35

    I'm not sure if I understood your question correctly, but on the controller you would have an action that handles the initial GET from the browser and a second action to handle POSTs.

     [AcceptVerbs(HttpVerbs.Post)]
     public ActionResult Create(MyModel model)
     {...}
    
     public ActionResult Create()
     {...}
    
    0 讨论(0)
  • 2020-12-09 17:38

    I would definitely take a look at this blog post by Scott Hanselman where he puts an aspx page in a MVC application.

    http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx

    Your controllers will not have access to the ViewState property. Even if you did want to handle the problem of __VIEWSTATE, you would have to do some work in order to get it into a usable form in an mvc controller. Good luck on coming up with a conversion strategy, no matter how it works out a lot of people would be interested to know kind of problems you would face in the process.

    0 讨论(0)
  • 2020-12-09 17:45

    If you have more than one form in an MVC page, you can add a hidden input within the form with a meaningful ID and test if it has a value. This way you do not need to have two separate handlers (one for get and one for post).

    So inf the page and inside the form:

     <input type="hidden" id="testForm" name="testForm" value="1"/>
    

    And in the controller :

    if (Request.Form["testForm"] != null)
            { 
            // ACTIONS FOR THE POSTED FORM
            }
    

    Hope it helps!

    0 讨论(0)
提交回复
热议问题