javascript/jquery modal popup dialog MVC 4 / render partial view

前端 未结 2 1156
心在旅途
心在旅途 2021-01-01 07:19

I have been using the DevExpress PopupControl. They look nice and pretty but they do not display the scrollbars on iOS/Android devices. So I want to come up with an alternat

相关标签:
2条回答
  • 2021-01-01 08:02

    Example with a static dialog (No AJAX)

    Define a div for your dialog content in a partial view:

    @model ClientDetail
    
    <h2>Client Detail</h2>
    @Html.DisplayFor(m => m.NameNumZone)
    @Html.DisplayFor(m => m.Birthdate)
     ...
    

    Dialog trigger and partial view:

    <a href="#" class="dialog-trigger" data-clientId="@p.Account">@p.NameNumZone</a>
    <div id="client-detail-modal">
        @Html.Partial("ClientDetail", Model.Item)
    </div>
    

    Javascript:

    $(document).ready(function() {
        // setup the dialog
        $("#client-detail-modal").dialog({
            modal: true,
            autoOpen: false,
            height: 100,
            width: 200
        });
    
        // bind the click event
        $(".dialog-trigger").on("click", function(event) {
            event.preventDefault();
            $("#client-detail-modal").dialog("open");  // show dialog
        });
    });
    

    Now if you have more than one client on a page you'll need a dialog per client. After a few clients it gets ugly. Instead, fill the dialog content dynamically.

    Dynamic dialog content (AJAX)

    Dialog container for your partial is empty initially:

    <div id="client-detail-modal"><!-- Client Partial, empty for now --></div>
    

    Get the partial via AJAX:

    $(".dialog-trigger").on("click", function(event) {
        event.preventDefault();
        var clientId = $(this).data("clientId");
        $.ajax({
            url: "Client/Details/" + clientId,
            type: "GET",
        })
        .done(function(result) {
            $("#client-detail-modal").html(result).dialog("open");
        });
    });
    

    Dynamic content (No AJAX)

    Another way to fill the dialog would be to populate the data attributes of the trigger element then replace content using javascript.

    <a href="#" class="dialog-trigger"
        data-clientId="@p.Account"
        data-birthdate="@p.Birthdate">@p.NameNumZone</a>
    
    $(".dialog-trigger").on("click", function(event) {
        var clientId = $(this).data("clientId");
        var birthdate = $(this).data("birthdate");
        // now replace content with new values
        $("span.birthdate").text(birthdate);
    });
    
    0 讨论(0)
  • 2021-01-01 08:03

    Put this content in your style sheet

            .modalDialog {
                position: fixed;
                font-family: Arial, Helvetica, sans-serif;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
                background: rgba(0,0,0,0.8);
                z-index: 99999;
                opacity:0;
                -webkit-transition: opacity 400ms ease-in;
                -moz-transition: opacity 400ms ease-in;
                transition: opacity 400ms ease-in;
                pointer-events: none;
            }
    
            .modalDialog:target {
                opacity:1;
                pointer-events: auto;
            }
    
            .modalDialog > div {
                width: 80%;
                position: relative;
                margin: 10% auto;
                padding: 5px 20px 13px 20px;
                border-radius: 10px;
                background: #fff;
                background: -moz-linear-gradient(#fff, #999);
                background: -webkit-linear-gradient(#fff, #999);
                background: -o-linear-gradient(#fff, #999);
            }
    
    
            .close {
                background: #606061;
                color: #FFFFFF;
                line-height: 25px;
                position: absolute;
                right: -12px;
                text-align: center;
                top: -10px;
                width: 24px;
                text-decoration: none;
                font-weight: bold;
                -webkit-border-radius: 12px;
                -moz-border-radius: 12px;
                border-radius: 12px;
                -moz-box-shadow: 1px 1px 3px #000;
                -webkit-box-shadow: 1px 1px 3px #000;
                box-shadow: 1px 1px 3px #000;
            }
    
            .close:hover { background: #00d9ff; }
    

    and in the code use the following

            <a href="#openModal" target="">Open Modal</a>
    
            <div id="openModal" class="modalDialog" data-theme="c">
                <div>
                    <a href="#close" title="Close" class="close" target="">X</a>
                    <h2>Pop up</h2>
                    <p>Pop up content. You can add your controls and content here</p>
                </div>
            </div>
    

    this logic worked for me. Hope it works for you also.

    Instead of using <a href="#close" title="Close" class="close" target="">X</a> for closing it is preferred you navigate to some parent page instead.

    0 讨论(0)
提交回复
热议问题