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

14 comments:


  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 | 192.168.1.1

    ReplyDelete
  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.
    http://run3play.com

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

    ReplyDelete
  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

    ReplyDelete
  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




    ReplyDelete
  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!

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

    DedicatedHosting4u.com


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

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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete