views:

274

answers:

2

I had an MVC 1.0 web application that was in VS 2008; I just upgraded the project to VS 2010 which automatically upgraded MVC to 2.0.

I have a bunch of viewpages have codebehind files that were manually added. The project worked fine before the upgrade, but now the onclick even't don't get triggered.

I.e. I have an asp:button with an onclick event that points to a method in the codebehind. When you click the button, the onclick event doesn't get triggered. In fact, when you look at the Page variable, IsPostBack is false.

This is really bizarre and I'm wondering if anyone know what happened and how to fix it. I'm thinking it has something to do with the changes in MVC 2.0; but I'm not sure.

Any help is really appreciated, I've been trying to figure this out for a while.

(deleting the codebehinds and moving that to the controller is not really an option since there is so many pages, moving back to vs 2008 is a last resort as I want to make use of some of the VS 2010 features like performance testing.)

+4  A: 

Using codebehind is a violation of the MVC pattern and is not supported. The fact that this worked in MVC 1 is accidental and unintended.

You noted that the project's a bit too large to remove the codebehind and move that logic to the controllers. As an alternative, if you find that the WebForms postback model is appropriate for your pages here, you may just want to make this part of your website WebForms (true .aspx without going through the MVC pipeline). This may be an easier task than trying to convert these pages to MVC. Remember that MVC and WebForms will coexist happily in the same application.

Otherwise, given your time constraints, your best bet is probably to revert to MVC 1 + VS2008.

Levi
+1 (actually a few days ago) - It's amazing how frequently a pattern gets abused, that abuse gets ingrained into a project, and then people end up being coded into what can only be described as a black hole.
Andras Zoltan
+2  A: 

Okay - so the fact that this project is in this mess is not something you can sort out; and is a conversation that presumably needs to be had with someone with ultimate responsibility for the project. As the other answer states - it's not supported and it should never be done.

All that aside, your question relates to the reason why it no longer works. I'm going to take a wild guess that it's because the viewstate and eventvalidation hidden inputs are not being sent back to the page. In Asp.Net 3.5 (therefore, possibly in Asp.Net 4), IsPostBack is detected from those two hidden values, not from the http method used to launch the page.

That said, your issue here could actually be that it's Asp.Net 4 that's not liking the incoming request, rather than MVC2 being the problem. There weren't that many changes made to how the view is rendered, just a lot of additions.

If the website has been upgraded to run Asp.Net 4 - see about reverting it back to 3.5 and see if that fixes it.

Otherwise, then I think that Levi's answer to revert back to 3.5 and MVC1 is your best bet.

Then I'd be recommending a full code review to get rid of this usage of MVC for future versions.

Andras Zoltan