Jquery Modal Popup opens twice on Single Click with ASP.Net MVC3

Posted by user1704379 on Stack Overflow See other posts from Stack Overflow or by user1704379
Published on 2012-10-19T19:53:39Z Indexed on 2012/10/22 23:00 UTC
Read the original article Hit count: 188

I am using Modal Popup in my MVC3 application it works fine but opens twice for a single Click on the link. The Modal pop is triggered from the 'Index' view of my Home Controller. I am calling a view 'PopUp.cshtml' in my modal popup. The related ActionMethod 'PopUp' for the respective view is in my 'Home' controller.

Here is the code,

Jquery code on layout.cshtml page,

<script type="text/javascript">
    $.ajaxSetup({ cache: false });

    $(document).ready(function () {
        $(".openPopup").live("click", function (e) {
            e.preventDefault();

            $("<div></div><p>")
                    .attr("id", $(this).attr("data-dialog-id"))
                    .appendTo("body")
                    .dialog({
                        autoOpen: true,
                        title: $(this).attr("data-dialog-title"),
                        modal: true,
                        height: 250,
                        width: 900,
                        left: 0,
                        buttons: { "Close": function () { $(this).dialog("close"); } }
                    })
                    .load(this.href);
        });

        $(".close").live("click", function (e) {
            e.preventDefault();
            $(this).dialog("close");
        });
    });
    </script>

cshtml code in 'PopUp.cshtml'

@{
    ViewBag.Title = "PopUp";
    Layout = null;
}

<h2>PopUp</h2>

<p>
Hello this is a Modal Pop-Up
</p>

Call modal popup code in Index view of Home Controller,

<p>
   @Html.ActionLink("Click here to open modal popup", "Popup", "Home",null, new { @class = "openPopup", data_dialog_id = "popuplDialog", data_dialog_title = "PopUp" })
</p>

What am I doing wrong that the modal pop up opens twice ?

Thanks in Advance !

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about asp.net-mvc-3