jquery - 在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">×</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 时,什么也没有发生。为什么?
解决方案
您可以尝试type="text/javascript"
在@section-scripts 中添加脚本标签。也许 ASP.net 强制执行 HTML 4 规范,其中“类型”是脚本标记的必需属性?
我不太了解 ASP.net 并将其添加为评论,但我没有足够的声誉。
推荐阅读
- r - R - 从数据框中删除差异小于 30 天的行
- python - 如何设置按钮 (command = def()) 的输出,作为 Combobox 值的输入 =
- python - 我正在尝试使用硒,但不断收到此错误
- c# - 带有 ID 的 GET 上的 OData ASP.Net Core?
- excel - 多个数据验证表更改
- flutter - Flutter CupertinoDatePicker - 我可以交换月份和日期的位置吗?
- tensorflow - 使用 Tensorflow keras loss=tf.losses.sparse_softmax_cross_entropy 时的奇怪准确度指标
- reactjs - 如何将移动手势(例如触摸和滑动)添加到 React Bootstrap 轮播组件?
- vba - 字符串值未正确传递
- jquery - jQuery在加载下一页后执行,不是当前的