【问题标题】:Modal Save Button Doesn't Work (Bootstrap 5)模态保存按钮不起作用(引导程序 5)
【发布时间】:2022-02-05 05:09:05
【问题描述】:

我正在关注本指南 (https://softdevpractice.com/blog/asp-net-core-mvc-ajax-modals/),但无法让我的模式上的保存按钮工作。我尝试将它放在表单标签中,但随后它使用了父视图中的表单操作而不是部分视图。

起初我无法让关闭按钮工作,但通过将data-dismiss 更改为data-bs-dismiss 解决了这个问题。我想知道我是否需要为保存做类似的事情,因为 Bootstrap 5 发生了变化。

我的代码如下:

父视图

<div id="AddStudyModal"></div>
// Irrelevant stuff here
<div>
   <button type="button" class="btn btn-secondary" data-toggle="ajax-modal" data-bs-target="#add-study" data-url="@Url.Action("AddStudy", "App", new {id = Model.GUID})">Add</button>
</div>

局部视图

<div class="modal fade" id="add-study" tabindex="-1" role="dialog" aria-labelledby="addStudyLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="addStudyLabel">Add Study</h5>
                <button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form asp-controller="App" asp-action="AddParticipant" method="post">
                    @Html.HiddenFor(m => m.ParticipantGuid)
                    <div asp-validation-summary="ModelOnly"></div>
                    <div class="mb-3 form-group">
                        <label asp-for="Studies" class="form-label"></label>
                        <select asp-for="SelectedStudyGuid" asp-items="Model.Studies" class="form-control" autocomplete="off">
                            <option value=""> Select a Study</option>
                        </select>
                        <span asp-validation-for="SelectedStudyGuid" class="text-danger"></span>
                    </div>
                    <div class="mb-3 form-group">
                        <label asp-for="StartDate" class="form-label"></label>
                        <input asp-for="StartDate" class="form-control" autocomplete="off"/>
                        <span asp-validation-for="StartDate" class="text-danger"></span>
                    </div>
                    <div class="mb-3 form-group">
                        <label asp-for="EndDate" class="form-label"></label>
                        <input asp-for="EndDate" class="form-control" autocomplete="off"/>
                        <span asp-validation-for="EndDate" class="text-danger"></span>
                    </div>
                    <div class="text-center">
                    </div>
                    <div class="text-center">@ViewBag.Message</div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" data-save="modal">Save</button>
            </div>
        </div>
    </div>
</div>

site.js

$(function (){
    var AddStudyElement = $('#AddStudyModal');
    $('button[data-toggle="ajax-modal"]').click(function(event){
        var url = $(this).data('url');
        $.get(url).done(function(data){
            AddStudyElement.html(data);
            AddStudyElement.find('.modal').modal('show');
        })
    })

    AddStudyElement.on('click', '[data-save="modal"]', function(event){
        event.preventDefault();

        var form = $(this).parents('.modal').find('form');
        var actionUrl = form.attr('action');
        var sendData = form.serialize();

        $.post(actionUrl, sendData).done(function(data){
            AddStudyElement.find('modal').modal('hide');
        })
    })
})

编辑:感谢@Efraim 在添加 .在模态之前。

【问题讨论】:

    标签: javascript asp.net-mvc asp.net-core bootstrap-modal bootstrap-5


    【解决方案1】:

    缺少一个'.'在: AddStudyElement.find('modal').modal('hide'); AddStudyElement.find('.modal').modal('hide');

    【讨论】:

    • 你是救生员 Efraim!我花了一个多小时试图弄清楚这一点,它是如此微小!保存按钮现在正在关闭模态,所以它是进步的,但现在我需要它去我的控制器。您是否认为 form.attr('action') 也存在问题?因为这是使用asp-action?
    • 已解决。我在那里采取了错误的行动。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多