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>).

Solution
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.

Solution
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);
                    }
                }
            }
            base.OnActionExecuting(filterContext);
        }
    }
}

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

11 comments:

  1. 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 https://form.com/form-software/offline-forms/. Maybe in the future, I will offer this to my colleagues.

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

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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: https://www.paydayloansnz.co.nz/instant-loans/

    ReplyDelete
  7. Bottlecaps | Learn all about Red Wines

    We at Bottlecaps are all about drinking wine and feeling fine. After all, we offer 360 degree e-commerce liquor solutions from where you can also order your favorite liquor online.

    ReplyDelete