ASP.NET MVC - Is IsPostBack still here?

2020-02-05 07:10发布

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 our site to MVC right now. So I am taking incremental steps, page by page, to convert them over while adding new features in MVC.

So my question is how can I access the IsPostBack property from a controller?

Edit: To further clarify, I have a webform user control on my mvc master page which can initiate postbacks. I'm trying to identify these postbacks verses an mvc post. At this point I think I am going to just check the request form keys for a "__viewstate" key and if its found treat it as a postback.

11条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-05 07:16

You can use this piece of code in Razor

@if(IsPost)
{
//dosomething
}
else
{
//do some other thing
}
查看更多
男人必须洒脱
3楼-- · 2020-02-05 07:16

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!

查看更多
Emotional °昔
4楼-- · 2020-02-05 07:17

In case anyone is still interested, you can test for a POST from inside an MVC Action Method like this:

if (Request.HttpMethod=="POST") { 

}
查看更多
劳资没心,怎么记你
5楼-- · 2020-02-05 07:18

The MVC framework doesn't support the classic postback and viewstate used in the Web forms. So, no, you don't have access to the IsPostBack.

My advice to you is to have two branches: one with the current site where you're adding patches for known errors and another one where you build a new site from scratch. New features should be implemented in this one. I assume that most of your codebase is re-usable in the new site.

When the new site is ready, put it in production.

查看更多
女痞
6楼-- · 2020-02-05 07:18

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.

查看更多
兄弟一词,经得起流年.
7楼-- · 2020-02-05 07:21

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()
 {...}
查看更多
登录 后发表回答