Search code examples
ajaxasp.net-mvc-3scriptingunobtrusive-javascriptunobtrusive

ASP .NET MVC 3 - Submitting an Ajax form redirects instead of updating within page


I'm submitting an Ajax form but when it hits the controller the Request.IsAjax returns false and my page therefore redirects. Anyone know why it would do this?

I have searched the internet and made sure that:

  1. I have the following in my web.config under <appSettings>:

    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

  2. I have the following scripts in my page at runtime from viewing the source:

    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript" />
    
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript" />
    
    <script src="/Scripts/jquery-ui-1.8.11.min.js" type="text/javascript" />
    
    <script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript" />
    
    <script src="/Scripts/jquery.validate.min.js" type="text/javascript" />
    
    <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript" />
    

Not sure if this is a red herring but I'll mention it because I've hit a brick wall here - when I do view source in Google Chrome only the first script has it's colours formatted, which makes me think I haven't got the syntax right - but it looks right to me! Help!


Solution

  • The problem is how I am submitting the form. I was just doing an onchange="submit()" in a dropdown list.

    I've now realised that a javascript submit like that is not the same as an ajax submit! :)

    If I use an <input type="submit" /> it works.