2

I'm trying to get this http://blog.stevehorn.cc/2009/06/rendering-modal-dialog-with-aspnet-mvc.html to work with MVC 3.

I'm pretty new at ASP.Net MVC, and started an MVC 3 project recently. I want to display a modal login form when the user click on the Log In hyperlink in my _Layout.cshtml file :

<a href="#" id="LogIn">Log In</a>

I've created a Login View in the following location Areas/Auth/Views/Auth/Login.cshtml

I've added the following script to to my _Layout.cshtml file:

    <script type="text/javascript">
        $(document).ready(function () 
        {
            $("#LogIn").click(function (event) 
            {
                $.get( 
                      "~/Areas/Auth/Views/Auth/Login" ,
                      function (htmlResult) 
                      {
                        $("#LoginModal").remove(); //In case this is the second time they've requested the dialog.
                        $("#container").append(htmlResult);
                        $("#LoginModal").dialog();
                      }
                    );
            return false; //To keep the default behavior of the anchor tag from occuring.
});                
</script>

And added a PartialViewResult to my AuthController:

        public PartialViewResult LoginView()
    {
        return PartialView("Login");
    }

However nothing happens when the login link is clicked. Any advice or links to a good MVC 3 tutorial on doing this, would be appreciated.

Thanks!

1
  • Knowing how MVC routes work is the crux of MVC, the magic, so to speak. Commented Feb 15, 2011 at 23:08

1 Answer 1

1

Your URI is wrong:

"~/Areas/Auth/Views/Auth/Login"

This is a reference to a view, not an action. Your URI needs to refer to an action. Also, JavaScript won't understand ASP.NET's ~ syntax. You probably want something like <%: Url.Content("~/Auth/Auth/LoginView") %> instead.

In the future, a good way to debug these things is Firebug's net panel or Fiddler. You should see the AJAX call returning a 404.

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

4 Comments

I've pointed it to my Login action, and in Fiddler I see itredirects to /login but nothing happens with the dialog
I can also see the html output in fiddler...is my javascript correct?
Ok, I've figured it out, I had no element with an id of container to append the result to:
specifeid in here $("#container").append(htmlResult); doh!

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.