views:

67

answers:

2

I am building a hybrid MVC/Webforms application where my MVC views are using an asp.net webforms 2 MasterPage. Everything has been working perfectly until I decided to put a form in my MVC view. Since ASP.NET Webforms wraps the entire page in a form element and you can't have a form within a form, I'm a little bit stuck.

There are a load of legacy controls in the master page which rely on the post back so I can't just disable it. The one thought I have at the moment is that all of the controls which rely on the aspnetForm form are above of the MVC page so was wondering if there is anyway to control when asp.net closes the aspnetForm form tag? Or does anyone have any other thoughts about how I could solve this problem?

+1  A: 

I've been here before, and ended up plumping for separate master pages, however:

Maybe you haven't looked too closely, but you do have control over where and how that form is wrapped around your entire container

[Edit]: More complete example:

<body>
<form id="form1" runat="server">
<div>
    <asp:ContentPlaceHolder ID="LegacyContentHolder" runat="server">

    </asp:ContentPlaceHolder>
</div>
</form>
<asp:ContentPlaceHolder ID="MvcContentPlaceHolder" runat="server">

 </asp:ContentPlaceHolder>    
</body>

Surely you can just move that around the appropriate placeholders for the Webforms stuff, and leave the other placeholders outside the scope of it?

This generates the following markup if used on a page

<body>
<form name="aspnetForm" method="post" action="Test.aspx" id="aspnetForm">
<div>
 <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"     value="/wEPDwUJNDMyNDU0NjAzZGT01Ocz+ON8w56SI8x7nj2/h8B/4g==" />
 </div>

<div>


Web forms behaviour has been generated here, boo

</div>
</form>


MVC content has been generated here, yay


</body>

[Edit]

Note: I've checked our code now, had to go back a few versions - what we also did is had three master pages, one for the skeleton described above, one with the specific ASP.NET Webforms stuff, and one inheriting from that for just the MVC stuff.

Kept things kinda clean, and ready for migrating it away from Webforms in the future.

Rob Ashton
A: 

You shouldn't have a problem, you control where the form tag is in your masterpage. Are you saying to need to close the masterpage's form tag early when using it for MVC, and leave it where it is for WebForms? You'll probably need to mess about with the control tree, when I've done this before ive used two masters, although you might be able to get away with abstracting a common base.master, then have mvc.master and webforms.master

Andrew Bullock