用.NETMVC调用Ajax
我正在尝试使用Ajax提交表单。但是当我提交时,页面会被重新加载,url也会改变。我认为url的改变是因为@Html.AntiForgeryToken()强>用.NETMVC调用Ajax,ajax,asp.net-ajax,Ajax,Asp.net Ajax,我正在尝试使用Ajax提交表单。但是当我提交时,页面会被重新加载,url也会改变。我认为url的改变是因为@Html.AntiForgeryToken() 见下面我的代码: 这是我的表单的外观: @model PersonModel .... <form action="SubmitLead" class="new-lead"> @Html.AntiForgeryToken(); <div class="col-md-12">
见下面我的代码: 这是我的表单的外观:
@model PersonModel
....
<form action="SubmitLead" class="new-lead">
@Html.AntiForgeryToken();
<div class="col-md-12">
<p>
<input type="hidden" value="@Model.TrackingCode" id="hdnTrackingCode" />
My name is @Html.TextBoxFor(model => model.FirstName,
new { @placeholder =
Html.DisplayNameFor(model => model.FirstName) })
@Html.TextBoxFor(model => model.Surname, new { @placeholder = Html.DisplayNameFor(model => model.Surname) })
</p>
</div>
<div class="clearfix"></div>
<div class="col-md-12 text-center">
<button type="submit" id="btnSubmit" class="orange-button">Get Quotes Now</button>
</div>
</form>
@if (Model.Results != null &&
Model.Results.IsSuccessful)
{
<div class="col-md-12 text-center">
<img src="~/Content/Images/Products/new-success.png" height="24px" />
<p id="result"></p>
</div>
}
@model PersonModel
....
@Html.AntiForgeryToken();
我的名字是@Html.TextBoxFor(model=>model.FirstName,
新的{@placeholder=
Html.DisplayNameFor(model=>model.FirstName)})
@Html.TextBoxFor(model=>model.nam姓氏,新的{@placeholder=Html.DisplayNameFor(model=>model.nam姓氏)})
立即获取报价
@如果(Model.Results!=null&&
Model.Results.IsSuccessful)
{
}
请在此处查看我的脚本:
@section Scripts{
<script type="text/javascript">
$(document).ready(function () {
$('.new-lead').submit(function (event) {
$.ajax({
url: '@Url.Action("Lead/SubmitLead")',
type: 'POST',
data: $(this).serialize(),
dataType: 'json',
success: function (result) {
var resultMessage = "success";
$('result').html(resultMessage);
}
})
})
})
</script>
@节脚本{
$(文档).ready(函数(){
$('.new lead')。提交(功能(事件){
$.ajax({
url:'@url.Action(“Lead/SubmitLead”)',
键入:“POST”,
数据:$(this).serialize(),
数据类型:“json”,
成功:功能(结果){
var resultMessage=“成功”;
$('result').html(resultMessage);
}
})
})
})
这样做
<form onsubmit="return submit(thi)" class="new-lead">
....
</form>
<script>
function submit(e){
$.ajax({
url: '@Url.Action("Lead/SubmitLead")',
type: 'POST',
data: $(e).serialize(),
dataType: 'json',
success: function (result) {
var resultMessage = "success";
$('result').html(resultMessage);
}
})
return false;
}
</script>
....
功能提交(e){
$.ajax({
url:'@url.Action(“Lead/SubmitLead”)',
键入:“POST”,
数据:$(e).serialize(),
数据类型:“json”,
成功:功能(结果){
var resultMessage=“成功”;
$('result').html(resultMessage);
}
})
返回false;
}