find a location for property in a new city

Monday, 14 June 2010

Make Form Postbacks work with MVC pages inside a WebForm MasterPage

I recently wrote about how I nested an MVC site with in a WebForms MasterPage and it how it all worked perfectly ;) However, I avoided the quite inevitable problem of POSTing not working as expected in your WebForm master page, mainly because I hadn't worked out how. Well I have now found a way and have explained it here:

Problem 1.

You may notice that any inputs above your MVC placeholder will work and any form inputs below your MVC placeholder will not POST.

The problem is that traditional ASP.NET web forms applications work off the principle of having only one form. MVC does not. However, if you look at the rendered HTML you may notice you have more than one form. ASP.NET will cope with the first form up until the first closing form tag (</form>).

Remove all form tags from your MVC pages that are causing the nested forms. This way there will only be one form per page and should therefore POST correctly. Your MVC pages will continue to POST as expected.

Problem 2.

So now your WebForms MasterPage and MVC page performs postbacks wherever there is a submit button as expected. The problem now is that you are going to have to handle that post back within your MVC page.

Make a base Controller class that all Controllers in the solution should inherit for. (I deliberately didn't end the class name with Controller so that MVC doesn't try and use it on its own).

Here is the main part of my ControllerBase:
using System.Web.Mvc;
namespace Web.Controllers
    public class ControllerBase : Controller
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
            //if there is a form being posted
            if (Request.Form != null && Request.Form.Count > 0)
                //iterate though all form keys
                for (var i = 0; i < Request.Form.Count; i++)
                    //get the value of the form key/value in question
                    var value = Request.Form[i];
                    //if this is the key/value form pair i want and it is not blank
                    if (Request.Form.Keys[i].EndsWith("txtSiteSearch") && !string.IsNullOrWhiteSpace(value))
                        //do something (this redirects to a page with the search value in the query string
                        Response.Redirect("/search/?q=" + value);

This will run on every request just before the selected ActionMethod is encountered.

I am essentially looking for a particular form field to check if the user typed into the MasterPage's site search text box (with ID="txtSiteSearch") before clicking a form submit button. If they have I send them to the search page with the search text in the querystring.

Obviously, this example is specific to my needs but if you need the form to use a service or other methods, these are all possible too.

Follow britishdev on Twitter


  1. The share your really gives us excitement. Thanks for your sharing. If you feel tired at work or study try to participate in our games to bring the most exciting feeling. Thank you!
    hotmail sign in | red ball 1 |

  2. I'm puzzled with lots of exercises. I was afraid I could not do the right time despite my hard work. I need a support person.

  3. Did I know about using forms before? Not exactly. Rather, when the use of mobile forms began and the need to master a huge amount of information, I understood the essence of this application Maybe in the future, I will offer this to my colleagues.

  4. Bài viết bạn rất hay:

    Chúng tôi là đơn vị cung cấp các sản phẩm chất lượng như:

    Giảo cổ lam

    giảo cổ lam giải độc gan

    giảo cổ lam giảm béo

  5. Bài viết bạn rất hay:

    Chúng tôi là đơn vị cung cấp các sản phẩm chất lượng như:

    lều xông hơi

    lều xông hơi sau sinh

    lều xông hơi giá bao nhiêu

  6. Bài viết rất hay: Chúng tôi chuyên cung cấp các sản phẩm chất lượng sau:

    bồn massage

    bon ngam chan

    máy massage chân

    Cảm ơn các bạn!

  7. Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks .

  8. برای بهترین, روان ترین و تخصصی ترین ترجمه مقالات ما را به خاطر داشته باشد,
    ترجمه تخصصی مقاله های تمامی رشته جهت ارسال به ژورنال های معتبر دنیا، بالاترین سطح ترجمه تخصصی را با ما تجربه کنید. همراه با کادری مجرب و کارکشته از بهترین مترجمتن سطح کشور، با تسلط کامل بر زبان انگلیسی و آشنایی کامل با فرمت نوشتاری مقالات تخصصی. دارای تجربه حداقل 10 سال در مترجمی زبان و رشته های تخصصی

  9. اگر به دنبال طراحی حرفه ای وب سایت خود بدون هیچ مشکلی هستید و میخواهید وب سایتی در حد طراحی فروشگاه اینترنتی دیجی کالا داشته باشید.کافیست با ما تماس بگیرید

  10. This is a very great post and the way you express your all post details that is too good.thanks for sharing with us this useful post..
    Field Engineer is an online marketplace that connects businesses who have jobs with Telecom Engineers who have the skills and availability to complete them..
    What Is Wireless Lan

  11. Managed IT Services are increasing sophisticated, cost-effective, empowering and constantly evolving, but there is one truth you can carve in stone: Managed IT Services are transforming business operations as they redefine profitability.
    managed service provider nj

  12. Permainan Sabung Ayam tentunya sudah pada tahu ya, yang dimana ayam melawan ayam pertandingan yang sangat seru ini bisa kalian nonton secara live lohh, banyak yang bermain di situs kami dan merasa sangat nyaman, bagi kalian yang ingin bermain bisa kunjungi situs kami, dijamin kalian akan merasa sangat senang.

  13. Permainan slot yang paling banyak diminati oleh semua orang, tentunya disitus kami dengan permainan slot terlengkap, pastinya akan membuat kalian betah permainan slot apa yang kalian dulu mainkan tentunya akan ada disini. silahkan kunjungi situs kami

    Agen Slot Vivo Indonesia

    agen resmi slot vivo Indonesia

    agen resmi slot vivo

    cara daftar slot vivo

    daftar akun vivo

    cara daftar vivo slot

    vivo slot

    daftar vivo tembak ikan

    Login Vivo Slot

    Deposit Pulsa Vivo Slot

    Withdraw vivo slot

    Livechat Vivo Slot Indonesia

    aplikasi vivo

  14. Marketing2business provides brand management solutions. We are committed to bringing ideas, inspiration, strategy, and tools to help our customers grow their business and achieve success.


  15. Great Article
    Cloud Computing Projects

    Networking Projects

    Final Year

    Projects for CSE

    JavaScript Training in Chennai


    Training in Chennai

    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,

    Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.

    Angular Training

  16. Once you’ve found your perfect freelancer crm, most of the hard work is over. The next step is to familiarize yourself with this new business software tool, and set it up in a way that will help you accomplish important business objectives.A good CRM will help you automatically log and act on email conversations with clients and prospective clients.

  17. Were you aware that you can get instant payday loans in New Zealand? The Payday Loans New Zealand blog introduces you to a simple and quick process to receive a cash loan with no fuss. This is a great article to read before Christmas for anyone who is struggling for cash to pay for Christmas presents for their families!

    If You Need Instant loans then visit: