在Ajax渲染局部视图中不显示引导程序工具提示

时间:2013-03-22 00:15:59

标签: ajax asp.net-mvc twitter-bootstrap asp.net-mvc-4

我在局部视图中有一个Bootstrap模式。在我的索引视图中,我单击一个按钮以在当前页面上显示局部视图模式。设置Jquery验证以在Bootstrap工具提示中显示验证错误。这些工具提示在所有页面上都可以显示,但在渲染的局部视图上。

我已经尝试将工具提示的z-index设置为大于模态,但这不起作用。

部分视图

@model Phase_3.ViewModels.UserCreateViewModel

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>New User</h3>
</div>
@using (Html.BeginForm("Create", "User", FormMethod.Post, new { @class = "remove-margin" }))
{
    <div class="modal-body">

        @Html.Label("First Name", new { @class = "inline" }) @Html.ValidationMessageFor(x => x.User.FirstName)
        @Html.TextBoxFor(x => x.User.FirstName, new { @class = "modal-input input-validation-error" })

        <br />
        @Html.Label("Last Name")
        @Html.TextBoxFor(x => x.User.LastName, new { @class = "modal-input" })
        <br />
        @Html.Label("Email")
        @Html.TextBoxFor(x => x.User.Email, new { @class = "modal-input" })
        <br />

        @* REMOVE WHEN RANDOM SALT METHOD IS COMPLETED*@
        @Html.Label("Salt")
        @Html.TextBoxFor(x => x.User.Salt, new { @class = "modal-input" })
        <br />
        @Html.Label("Password")
        @Html.TextBoxFor(x => x.User.Password, new { @class = "modal-input" })
        <br />
        @Html.Label("Role")
        @Html.DropDownListFor(x => x.User.RoleId, new SelectList(Model.Roles, "Id", "Title"), new { @class = "row-fluid" })
    </div>

    <div class="modal-footer">
        <a href="#" class="btn" data-dismiss="modal">Close</a>
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
    @Scripts.Render("~/bundles/jqueryval")
}

索引视图

@model IEnumerable<Phase_3.Models.User>
<script src="/Content/custom/js/display.modal.js"></script>

<div class="row-fluid spacer">
    <div id="modal-container" class="modal hide fade" data-url='@Url.Action("Create")'>
        <div id="modal-inner"></div>
    </div>

<input type="button" id="display-modal" value="New User" class="btn btn-primary" />
</div>

@* Display users in data table *@
<div class="row-fluid">

    <table class="table table-condensed table-hover">
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Role</th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(x => item.FirstName)</td>
                <td>@Html.DisplayFor(x => item.LastName)</td>
                <td>@Html.DisplayFor(x => item.Email)</td>
                <td>@Html.DisplayFor(x => item.Role.Title)</td>
            </tr>
        }
    </table>
</div>

在工具提示中显示错误

$.validator.setDefaults({
showErrors: function (errorMap, errorList) {
    this.defaultShowErrors();

    // destroy tooltips on valid elements                              
    $("." + this.settings.validClass)
        .tooltip("destroy");

    // add/update tooltips 
    for (var i = 0; i < errorList.length; i++) {
        var error = errorList[i];

        $("#" + error.element.id)
            .tooltip({ trigger: "focus" })
            .attr("data-original-title", error.message)
    }
}
});

1 个答案:

答案 0 :(得分:1)

此问题可能是由于您在初始加载页面时初始化工具提示(运行$.validator.setDefaults代码)一次。确保在ajax调用后重新初始化所有工具提示:在success函数中。