【问题标题】:ASP .NET MVC 5 + Bootstrap modal with form POST带有 POST 表单的 ASP .NET MVC 5 + Bootstrap 模式
【发布时间】:2016-10-18 12:07:35
【问题描述】:

我在 ASP .NET MVC 5 Web 应用程序中发布引导模式时遇到问题。 我正在使用模态的部分视图:

    @model Presentation.APP.MyAPP.Portal.Models.DocumentAndFilesManager.AddDocumentTypeModel

<script>
    $(document).ready(function () {
        $("#addDocumentTypeModal").on("submit", "#form-adddocumenttype-appt", function (e) {
            e.preventDefault();  // prevent standard form submission

            var form = $(this);
            $.ajax({
                url: form.attr("action"),
                method: form.attr("method"),  // post
                data: form.serialize(),
                success: function (partialResult) {
                    $('#addDocumentTypeModal').modal('toggle');
                    $("#form-container").html(partialResult);
                }
            });
        });
    });
</script>

<div class="modal fade" id="addDocumentTypeModal" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h3 class="modal-title" id="addDocumentTypeModalLabel">Add document type</h3>
            </div>

            @using (Html.BeginForm("AddDocumentType", "WebManager", FormMethod.Post, new { id = "form-adddocumenttype-appt" }))
            {
                <div class="modal-body" id="form-container">
                    <div class="row">
                        <div class="col-sm-12">
                            Input unique NAME and unique REFERENCE NUMBER of new Document.
                        </div>
                    </div>

                    <div class="row">

                        <br />

                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                        <div class="form-group">
                            @Html.LabelFor(m => m.DocumentTypeTitle, new { @class = "col-sm-12 control-label" })
                            <div class="col-sm-8">
                                @Html.TextBoxFor(m => m.DocumentTypeTitle, new { @class = "form-control" })
                                @Html.ValidationMessageFor(m => m.DocumentTypeTitle, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(m => m.DocumentTypeReferenceNumber, new { @class = "col-sm-12 control-label" })
                            <div class="col-sm-8">
                                @Html.TextBoxFor(m => m.DocumentTypeReferenceNumber, new { @class = "form-control" })
                                @Html.ValidationMessageFor(m => m.DocumentTypeReferenceNumber, "", new { @class = "text-danger" })
                            </div>
                        </div>

                        <div class="modal-footer">
                            <button type="submit" class="btn btn-success">Add</button>
                            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>

然后我有渲染部分视图的父视图:

<div id="form-container">
@Html.Partial("_AddDocumentTypeModal")
</div>

最后是我的控制器操作方法:

[HttpPost]
    public ActionResult AddDocumentType(AddDocumentTypeModel model) {
        if (ModelState.IsValid) {
            if (!documentManagementFacade.AddDocumentType(model.DocumentTypeReferenceNumber, model.DocumentTypeTitle)) {
                ModelState.AddModelError("", "Document type with this title and/or reference number already exists!");
            }
            else
            {
                return Json(new { success = true });
            }
        }

        return PartialView("_AddDocumentTypeModal", model);
    }

表单中的验证工作正常,但发布后模式存在问题:成功发布后模式仍然存在以及发布错误后。 成功发布后如何在发布错误/隐藏模式后显示一些消息?

非常感谢。

【问题讨论】:

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


    【解决方案1】:
    $.ajax({
     url: form.attr("action"),
     method: form.attr("method"),  // post
     data: form.serialize(),
     success: function (partialResult) {
           $('#addDocumentTypeModal').modal('hide');
           $('#addDocumentTypeModal').find("#errorMsg").hide();
           $("#form-container").html(partialResult);
    
         }
     error:function (error) {
           $('#addDocumentTypeModal').find("#errorMsg").html(error).show();
         }
    

    });

    在modal body里面添加一个error div

    <div class="modal-body" id="form-container">
      <div class="alert alert-danger" id="errorMsg" style="display:none" >Oops! Some error.....</div>
      ....
      ....
    </div>
    

    【讨论】:

    • 现在它在成功调用后工作 - 模式已关闭。但同样的情况是在发布错误后。在重新打开模态(不刷新父页面)之后,有一些修改过的模态 - 没有正文只是模态头。
    • 不要使用@using (Html.BeginForm("AddDocumentType", "WebManager", FormMethod.Post, new { id = "form-adddocumenttype-appt" })),因为你通过ajax发送表单,不要使用&lt;button type='submit',只使用&lt;button
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2014-04-26
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多