【问题标题】:Pass data model through ajax post in MVC在MVC中通过ajax post传递数据模型
【发布时间】:2019-10-29 22:15:19
【问题描述】:

我有部分模式视图:

<div class="container">
    <div class="row">
        <div class="modal-dialog">
            <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" id="myModal-label">Bootstrap Dialog</h4>
                </div>
                <div class="modal-body">
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                        </div>
                    </div>
                    <div class="form-group">
                        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                        </div>
                    </div>

                    <div class="form-group">
                        @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn-primary btn" id="btnOK" onclick="">OK</button>
                    <button type="button" class="btn-default btn" data-dismiss="modal" id="btnCancel" onclick="">Cancel</button>
                </div>
            </div>
        </div>
    </div>
</div>
@section scripts{
    <script>
        $(document).ready(function () {
            $('#btnOK').click(function () {
                var data = {Name: "dsdsd", Email: "dsdsd@sdsd.com", Mobile: 05456545}
                //$.post(url, data)
                console.log(data);
            });
        })
    </script>
}

但是当我点击 OK 按钮时没有任何反应,我的控制台中没有任何内容。 我希望能够将我的数据传递给我的控制器,然后将一些成功消息显示为另一个弹出窗口或其他内容。

所以,要明确一点,在主页上使用此代码打开模态即可:

<button type="button" class="btn-block">Modal</button>
<div class="modal fade" id="myModal" role="dialog" data-url='@Url.Action("_ContactForm","Home")'></div>

@section scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $('.btn-block').click(function () {
                var url = $('#myModal').data('url');

                $.get(url, function (data) {
                    $('#myModal').html(data);
                    $('#myModal').modal('show');
                });
            });
        });
    </script>
}

但是当我在 modal 上点击 OK 时,什么也没有发生。为什么?

【问题讨论】:

  • 所以你的意思是$('#btnOK').click 不会触发?如果它在局部视图中,并且您通过 AJAX 加载了局部视图,则它将无法工作,因为通过 AJAX 下载的脚本不会被执行(主要是出于安全原因)。

标签: jquery html asp.net-mvc


【解决方案1】:

您可以尝试将type="text/javascript" 添加到@section-scripts 中的脚本标签。 也许 ASP.net 强制执行 HTML 4 规范,其中 'type' 是 script 标签的必需属性?

我对 ASP.net 了解不多,会将此作为评论添加,但我没有足够的声誉。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多