如何在另一个控制器中调用部分视图时正确使用div.load?

时间:2017-05-05 08:15:38

标签: javascript jquery asp.net-mvc

所以我有这个代码调用包含局部视图的弹出对话框。问题是每当我调用对话框时,div.load会将整个字符串块附加到主控制器的地址。

function OpenSendMessagePopUp() {
    var div = $("#DivAppendToPartialView");
    div.load("@Url.Content(~/Resend/_SendMessage.cshtml)", function () {
        div.dialog({
            modal: true,
            width: 500,
            height: 420,
            show: 'blind',
            hide: 'blind',
            title: "Send Message",
            draggable: false,
            resizable: false,
            buttons: {
                "Cancel": function () {
                    $(this).dialog("close");
                }
            }
        });
     
    });
}

function SaveMessage() {
    $.ajax({
        type: 'POST',
        url: '@Url.Content("~/Resend/_SendMessage.cshtml")',
        data: {
            smsContentId: smsContentId,
            comments: comments
        },
    });

MobileNumberUpload是项目的主控制器,而Resend是部分视图的控制器。请注意,我不能根据项目的约束组合两个控制器。

Error Message for when i click the button

This is the dialog that pops up when i click the button

1 个答案:

答案 0 :(得分:0)

尝试以下更改:

function OpenSendMessagePopUp() {
    var div = $("#DivAppendToPartialView");
    div.load('@Url.Content("~/Resend/_SendMessage.cshtml")', function () {
        div.dialog({
            modal: true,
            width: 500,
            height: 420,
            show: 'blind',
            hide: 'blind',
            title: "Send Message",
            draggable: false,
            resizable: false,
            buttons: {
                "Cancel": function () {
                    $(this).dialog("close");
                }
            }
        });

    });
}

function SaveMessage() {
$.ajax({
    type: 'POST',
    url: '@Url.Content("~/Resend/_SendMessage.cshtml")',
    data: {
        "smsContentId": smsContentId,
        "comments": comments
    },
});
}

还要确保在JS中声明 smsContentId 评论