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

前端 未结 2 1175
心在旅途
心在旅途 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
    
    

    Client Detail

    @Html.DisplayFor(m => m.NameNumZone) @Html.DisplayFor(m => m.Birthdate) ...

    Dialog trigger and partial view:

    @p.NameNumZone
    
    @Html.Partial("ClientDetail", Model.Item)

    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:

    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.

    @p.NameNumZone
    
    $(".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);
    });
    

提交回复
热议问题