当我使用对话框表格进行编辑

时间:2017-04-06 18:11:47

标签: javascript twitter-bootstrap asp.net-mvc-5

我尝试使用插件 https://bunjeeb.com/2014/06/03/mvc-tooltip-validation-using-jquery-and-bootstrap/

但验证错误第一次出现在EditControl下,当我按下“保存”按钮时消失,然后当我第二次按下“保存”按钮时就像工具提示一样

请帮帮我 非常感谢

1。 _Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Moving Pro Services</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <link href="@Url.Content("~/Content/jquery.validate.mvc.css")" rel="stylesheet" type="text/css"/>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
    <body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        @if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("Moving Pro Services", "Index", "Home", new {area = ""}, new {@class = "navbar-brand"})
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Services <span class="caret"></span></a>
                            <ul class="dropdown-menu" role="menu">
                                <li>@Html.ActionLink("Companies", "Index", "Companies")</li>
                                <li>@Html.ActionLink("App Versions", "Index", "AppVersions")</li>
                                @if (User.Identity.IsAuthenticated)
                                {
                                    if (User.IsInRole("Administrator"))
                                    {
                                        <li>@Html.ActionLink("Create User", "Register", "Account")</li>
                                    }
                                }
                            </ul>
                        </li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    </ul>
                    @Html.Partial("_LoginPartial")
                </div>
            </div>
        }
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr/>
        <footer>
            <p>&copy; @DateTime.Now.Year - Moving Pro Services</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)

    <div id='myModal' class='modal fade in'>
        <div class="modal-dialog">
            <div class="modal-content">
                <div id='myModalContent'></div>
            </div>
        </div>
    </div>

    </body>
</html>

2。查看

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - Moving Pro Services</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
    <link href="@Url.Content("~/Content/jquery.validate.mvc.css")" rel="stylesheet" type="text/css"/>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
</head>
    <body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        @if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    @Html.ActionLink("Moving Pro Services", "Index", "Home", new {area = ""}, new {@class = "navbar-brand"})
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Services <span class="caret"></span></a>
                            <ul class="dropdown-menu" role="menu">
                                <li>@Html.ActionLink("Companies", "Index", "Companies")</li>
                                <li>@Html.ActionLink("App Versions", "Index", "AppVersions")</li>
                                @if (User.Identity.IsAuthenticated)
                                {
                                    if (User.IsInRole("Administrator"))
                                    {
                                        <li>@Html.ActionLink("Create User", "Register", "Account")</li>
                                    }
                                }
                            </ul>
                        </li>
                        <li>@Html.ActionLink("About", "About", "Home")</li>
                        <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
                    </ul>
                    @Html.Partial("_LoginPartial")
                </div>
            </div>
        }
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr/>
        <footer>
            <p>&copy; @DateTime.Now.Year - Moving Pro Services</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)

    <div id='myModal' class='modal fade in'>
        <div class="modal-dialog">
            <div class="modal-content">
                <div id='myModalContent'></div>
            </div>
        </div>
    </div>

    </body>
</html>

第3。用于创建编辑

的对话框的脚本
@model Maintenance.Models.AppVersion


@using (Html.BeginForm())
{
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Edit App Version</h4>
        </div>

        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.AppVersion1)

        <div class="modal-body">
            <div class="form-horizontal">
                <div class="form-group margin-bottom">
                    @Html.LabelFor(model => model.AppVersionDescription, htmlAttributes: new { @class = "control-label col-md-4" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.AppVersionDescription, new { htmlAttributes = new { @class = "form-control", autofocus = "" } })
                        @Html.ValidationMessageFor(model => model.AppVersionDescription)
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <input class="btn btn-primary" type="submit" value="Save changes" id="approve-btn" />
            <button class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
}

@section Scripts {

    @Scripts.Render("~/bundles/jqueryval")

    <script>
        $(function () {
            $("form input").tooltipValidation({
                placement: "right"
            });
        });
    </script>
}

4。模型

@model Maintenance.Models.AppVersion


@using (Html.BeginForm())
{
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title">Edit App Version</h4>
        </div>

        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.AppVersion1)

        <div class="modal-body">
            <div class="form-horizontal">
                <div class="form-group margin-bottom">
                    @Html.LabelFor(model => model.AppVersionDescription, htmlAttributes: new { @class = "control-label col-md-4" })
                    <div class="col-md-8">
                        @Html.EditorFor(model => model.AppVersionDescription, new { htmlAttributes = new { @class = "form-control", autofocus = "" } })
                        @Html.ValidationMessageFor(model => model.AppVersionDescription)
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <input class="btn btn-primary" type="submit" value="Save changes" id="approve-btn" />
            <button class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
    </div>
}

@section Scripts {

    @Scripts.Render("~/bundles/jqueryval")

    <script>
        $(function () {
            $("form input").tooltipValidation({
                placement: "right"
            });
        });
    </script>
}

5。用于显示工具提示的插件

$(function () {
    $.ajaxSetup({ cache: false });

    $(document).on("click", ".modal-link", function (e) {
        $('#myModalContent').load(this.href, function () {
            $('#myModal').modal
                (
                {
                    keyboard: true
                }, 'show'
                );

            bindForm(this);
        });
        return false;
    });

    $(document).on("click", "a[data-modal]", function (e)
    {
        $('#myModalContent').load(this.href, function ()
        {
            $('#myModal').modal
            (
                {
                  keyboard: true
                }, 'show'
            );

            bindForm(this);
        });
        return false;
    });

    $(window.document).on('shown.bs.modal', '.modal', function () {
        window.setTimeout(function () {
            $('[autofocus]', this).focus();
        }.bind(this), 100);
    });
});


function bindForm(dialog) {
    $('form', dialog).submit(function () {
        var isValid = true; // assume all OK
        $('form').validate(); // perform validation on the form
        $('input[type="text"]').each(function (index, item) { // could change selector to suit e.g $('input, textarea').each(..            
            if (!$(this).valid()) {
                isValid = false; // signal errors
                return false; // break out of loop   
            }
        });
        if (!isValid) {
            return false; // exit
        }
        $('#progress').show();
        $.ajax({
            url: this.action,
            modal: true,
            draggable: true,
            resizable: false,
            type: this.method,
            data: $(this).serialize(),
            success: function (result) {

                    $('#myModal').modal('hide');
                    $('#progress').hide();
                    //location.reload();
                    alert(result.message);
            }
        });
        return false;
    });
}

0 个答案:

没有答案