使用JQuery使用MVC4 partialview填充模式弹出窗口

时间:2015-07-19 17:33:37

标签: jquery asp.net-mvc-4 jquery-ui modal-dialog

这让我疯了。我在解决这个问题上遇到了问题。对话框打开,但未加载部分视图。我在这里尝试了许多例子,但无济于事。这是我到目前为止所拥有的。

我的_layout.cshtml文件包含以下内容:

<div id="modal-content" name="modal-content"></div>

在我的site.js中,我有以下内容:

$('#modal-content').dialog({
    autoOpen: false,
    position: { my: 'center', at: 'top+350', of: window },
    width: 'auto',
    height: 'auto',
    fluid: true,
    modal: true,
    resizable: false,
    title: 'Create Guestbook Post',
    open: function(event, ui) {
        $(this).load('@Url.Action("_Create")');
    },
});

$('#buttonCreateGuestbookPost').click(function() {
    $('#modal-content').dialog('open');
});

在partialview(_Create.cshtml)中,我有以下内容:

@model MPP.Database.Guestbook
@using (Html.BeginForm())
{
    <div class="row">
        <h2>Create Account</h2>
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <p>
            @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label float-left" })
            @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger float-right" })
            @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
        </p>
        <p>
            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label float-left" })
            @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control float-right" } })
            @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
        </p>
        <p>
            @Html.LabelFor(model => model.EmailAddress, htmlAttributes: new { @class = "control-label float-left" })
            @Html.EditorFor(model => model.EmailAddress, new { htmlAttributes = new { @class = "form-control float-right" } })
            @Html.ValidationMessageFor(model => model.EmailAddress, "", new { @class = "text-danger" })
        </p>
        <p>
            @Html.LabelFor(model => model.Body, htmlAttributes: new { @class = "control-label float-left" })
            @Html.EditorFor(model => model.Body, new { htmlAttributes = new { @class = "form-control float-right" } })
            @Html.ValidationMessageFor(model => model.Body, "", new { @class = "text-danger" })
        </p>
        <p>
            <input type="submit" id="buttonCreateGuestbook" name="buttonCreateGuestbook" class="btn btn-primary" value="Create" />
        </p>
        }
    </div>
}

在我希望弹出窗口显示的视图中,我有以下按钮:

<a href="javascript:void(0);" class="btn btn-xs btn-success" id="buttonCreateGuestbookPost" style="margin-bottom:3px;">Post to Guestbook</a>

当加载留言板页面时,所有组件都正确加载(打开对话框的按钮,模式内容DIV,打开/加载对话框的javascript等)。问题是,当单击该按钮时,模态对话框将打开,但不会加载部分视图。

非常感谢任何帮助。感谢。

1 个答案:

答案 0 :(得分:1)

它说代码在site.js,但你在那里使用Razor代码作为网址。

JS文件是静态的,不会被Razor编译。对硬件进行硬编码或将其作为变量传递到视图中,以便脚本可以访问该变量。