Jquery 在MVC中通过ajax post传递数据模型
我有部分模态视图:Jquery 在MVC中通过ajax post传递数据模型,jquery,html,asp.net-mvc,Jquery,Html,Asp.net Mvc,我有部分模态视图: <div class="container"> <div class="row"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" c
<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>
}
&时代;
引导对话框
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”})
@LabelFor(model=>model.Email,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Email,“,new{@class=“text danger”})
@LabelFor(model=>model.Mobile,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Mobile,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Mobile,“,new{@class=“text danger”})
好啊
取消
@节脚本{
$(文档).ready(函数(){
$('#btnOK')。单击(函数(){
var数据={Name:“DSD”,电子邮件:dsdsd@sdsd.com“,手机号码:05456545}
//$.post(url、数据)
控制台日志(数据);
});
})
}
但当我按下OK按钮时,什么也没发生,我的控制台里什么也没有。
我希望能够将我的数据传递给我的控制器,然后以另一个弹出窗口或其他方式显示一些成功消息
因此,为了清楚起见,请在主页上使用以下代码打开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>
}
模态
@节脚本{
$(文档).ready(函数(){
$('.btn块')。单击(函数(){
var url=$('#myModal')。数据('url');
$.get(url、函数(数据){
$('#myModal').html(数据);
$('myModal').modal('show');
});
});
});
}
但当我在模态上点击OK时,什么也没发生。为什么?您可以尝试将
type=“text/javascript”
添加到@section scripts内的脚本标记中。
也许ASP.net强制执行HTML4规范,其中“type”是脚本标记的必需属性
我对ASP.net了解不多,我想把它作为一个评论,但我没有足够的声誉。那么你是说$(“#btnOK”)。单击不会触发吗?如果这在部分视图中,并且您通过AJAX加载了部分视图,那么它将无法工作,因为通过AJAX下载的脚本不会执行(主要是出于安全原因)。