首页 > 解决方案 > 在MVC中通过ajax post传递数据模型

问题描述

我有部分模式视图:

<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>
}

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

标签: jqueryhtmlasp.net-mvc

解决方案


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

我不太了解 ASP.net 并将其添加为评论,但我没有足够的声誉。


推荐阅读