1

Anyone else having trouble with MVC3, JQuery 1.7.1 and JQuery Mobile 1.1.0? Whenever I submit a standard MVC form my URL goes from:

localhost/site/controller/action

To

localhost/site/controller/action#/controller/action

I'm in the middle of simplifying an existing site that is quite messy so am working through controller by controller cleaning up and removing jquery form handling where it isn't required and just using Razor. I wondered if something in my project was conflicting but i've managed to replicate it from a new project:

Open VS2010, New Empty MVC3 Project - i.e. everything not mentioned below is the default MVC project

Add TestController:

using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class TestController : Controller
     {
        public ActionResult Testing()
        {
            return View(new TestModel());
        }

        [HttpPost]
        public ActionResult Testing(TestModel testModel)
        {
            if (ModelState.IsValid)
            {
                //Temp for examples sake
                ModelState.AddModelError("EmailAddress", "Account not found.");
            }

            return View();
        } 
    }
}

Add TestModel:

using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Models
{
    public class TestModel
    {
        [Required(ErrorMessage = "Email is required.")]
        [DataType(DataType.EmailAddress)]
        [StringLength(50, ErrorMessage = "Email too long.")]
        public string EmailAddress { get; set; }
    }
}

Add view Folder Test with view Testing:

@model MvcApplication1.Models.TestModel

@using (Html.BeginForm("Testing", "Test", FormMethod.Post))
{ 
    <div id="divForgotPassword">
        <fieldset data-role="fieldcontain">
            <legend>Forgot Password</legend>
            <div id="editor-label">
                <label for="txtLogonID">
                    Email Address:</label></div>
            <div id="editor-field">
                @Html.TextBoxFor(m => m.EmailAddress, new { type = "email" })
                <br />
                @Html.ValidationMessageFor(m => m.EmailAddress)</div>
            <input type="submit" value="Reset Password" data-role="button" data-inline="true" />
        </fieldset>
    </div>
}

Run and visit http://localhost:65298/Test/Testing Hit Reset Password, validator works, URL doesn't change

Add jquery.mobile-1.1.0.js and jquery-1.7.1.min.js to scripts folder

To _Layout.cshtml add: And Change jquery 1.5.1 to 1.7.1

Run and visit our page again Hit Reset Password, validator works but now you have: http://localhost:65298/Test/Testing#/Test/Testing

I must be doing something wrong?? The main problem with this is that with that URL other javascript I have doesn't like to run. I also worry it will interefere with something else, plus it looks ugly and has to be wrong...

Many Thanks In Advance!!

2 Answers 2

3

As stated, loading pages through ajax is the default behavior of jQm. Using rel="external" does work for links, but it is actually meant for links that are external to the scope of your mobile app (like visiting a "main" non-mobile site hosted on the same domain), and it does not work for form submissions. For individual links that are in scope to your mobile app and/or form submissions, adding the attribute data-ajax="false" is the recommended approach.

So, for individual links, you would do this:

<a href="/nonMobileHome" rel="external">Full website (non-mobile)</a>
<a href="/user/edit/5" data-ajax="false">Load mobile page without ajax</a> 

For a form submission, use:

<form action="/user/edit/5" method="post" data-ajax="false">

For all links/forms on a page, add this javascript to your page, before you load jQuery Mobile:

$(document).bind("mobileinit", function () { $.mobile.ajaxEnabled = false; });

I've found that it's easiest within ASP.NET MVC to just disable jQm's ajax loading behavior globally, by including the javascript statement above in a site-wide js file. Unfortunately this problem still persists in MVC4 and Visual Studio 2012, even though Microsoft includes a jQuery Mobile MVC4 template by default.

More information on suppressing ajax loading in jQuery Mobile: http://jquerymobile.com/test/docs/pages/page-links.html
http://jquerymobile.com/test/docs/forms/forms-sample.html

Sign up to request clarification or add additional context in comments.

Comments

2

That is the default behaviour of jQuery mobile. For all links, instead of loading a page in normal way, It sends an ajax request and load the page and then update(just append) the url.

If you want to disable this behaviour, you can put rel=external in the link and the link will load without ajax and your url will then be usual.

<a href="User/somepage" rel="external" />I wont be using ajax for navigation</a>

http://jquerymobile.com/demos/1.0a4.1/#docs/pages/docs-navmodel.html

1 Comment

Thanks I think I have it working now! I read along those lines and realised that I needed to define the form with: @using (Html.BeginForm("Reset", "ForgotPassword", FormMethod.Post, new { data_ajax = "false" }))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.