Ajax 提交会在 MVC 中自动调用
本文关键字:调用 MVC 提交 Ajax | 更新日期: 2023-09-27 18:32:59
我有一个奇怪的问题。这是我的jquery代码
$("#btnRate").click(function (e) {
alert("tık");
e.preventDefault();
var electionId = '@Model.ElectionId';
var profileId = '@Model.ProfileId';
$.ajax({
url: "Profile/Vote", // '@Html.Action("Vote","Profile")',
// data: { electionId: electionId, profileId: profileId },
dataType: "json",
type: "POST",
error: function (error) {
alert('An error occured, please try again! ');
},
success: function (data) {
if (data != null && data.success) {
alert("s1");
alert(data.url);
alert("s2");
window.location = data.url;
} else {
alert('An error occured, please try again. ');
}
}
});
return false;
});
这是 HTML 端代码
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="btnRate" type="submit" class="btn btn-default" value="Vote !" />
</div>
</div>
加载详细信息表单时,将自动调用为单击的 btnRate 按钮。但我不点击。.
这是配置文件控制器中的投票操作
// [HttpPost]
[ChildActionOnly]
public JsonResult Vote() //(int profileId, int electionId)
{
EvoteServicesProviderClient service = new EvoteServicesProviderClient();
// var result= service.createPolls(electionId, profileId);
// if(result ==1)
// return Json(new { success = true, url = "/Home/ProfileStatistic?electionId=" + electionId }, JsonRequestBehavior.AllowGet);
// else
// return Json(new { success = false, url = "/Home/ProfileStatistic?electionId=" + electionId }, JsonRequestBehavior.AllowGet);
return null;
}
即使我不点击,投票函数也被 Ajax 调用。原因是什么?
编辑:这是例外
类型为"System.Web.HttpException"的异常发生在 System.Web.dll但未在用户代码中处理
其他信息:执行处理程序的子请求时出错 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.
我解决了这个问题..非常有趣.. 我刚刚删除了评论 '@Html.Action("投票","个人资料")',
现在效果很好.. 这是最后一部分。.
$(document).ready(function () {
$("#btnRate").click(function(e) {
alert("geldi");
alert("tık");
e.preventDefault();
var electionId = '@Model.ElectionId';
var profileId = '@Model.ProfileId';
$.ajax({
url: '@Url.Action("Vote","Profile")',
data: { electionId: electionId, profileId: profileId },
dataType: "json",
type: "POST",
error: function (error) {
alert('An error occured, please try again! ');
},
success: function (data) {
if (data != null && data.success) {
alert("s1");
alert(data.url);
alert("s2");
window.location = data.url;
} else {
alert('An error occured, please try again. ');
}
}
});
return false;
});
});