gpt4 book ai didi

javascript - Ajax.BeginForm OnSuccess、onBegin、OnFailure 未触发

转载 作者:行者123 更新时间:2023-12-03 02:25:55 25 4
gpt4 key购买 nike

无论我尝试什么,没有一个 JavaScript 方法不能触发/工作。

[HttpPost]
public JsonResult Errors (string name, string email)
{
//send email
return new JsonResult()
{
Data = new { result = "success" },
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}

部分 View

@using (Ajax.BeginForm(
"Errors",
"Home",
new AjaxOptions {
HttpMethod = "POST",
OnSuccess = "onSuccess ",
OnFailure = "OnFailure",
OnBegin = "Begin()" },
new { @class = "form-horizontal", id = "error-form" }))
{
@Html.AntiForgeryToken()

<div class="form-group">
<label>Your name</label>
<input type="text" name="name" class="form-control" value="@name" />
</div>
<div class="form-group">
<label>Your e-mail</label>
<input type="text" name="email" class="form-control" value="@email" />
</div>



<button class="btn btn-success" type="submit" style="margin-bottom:4em;">Send message</button>
}

<div class="form-group" id="thankYou" style="display:none">
<label>Thank you!</label>
</div>

</div>
</div>
</div>

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script type="text/javascript">
function Begin() {
alert("b");
}

function onSuccess(data) {
debugger;
alert(data.result);
};

function OnFailure(data) {
debugger;
alert(data.result);
};

$(document).ready(function () {

});

</script>

我得到的只是 JSON 格式的响应。请参阅screenshot 。没有事件被触发。有什么想法吗?

最佳答案

Ajax.BeginForm有 11 个重载...您的参数与其中任何一个都不匹配。我假设您想使用 this overload :

BeginForm(AjaxHelper, String, String, RouteValueDictionary, AjaxOptions, IDictionary<String,Object>)

您需要将 RouteValueDictionary 添加到您的参数中:

@using (Ajax.BeginForm(
"Errors", // <--action name
"Home", // <--controller
null, // <-- RouteValueDictionary
new AjaxOptions { // <-- AjaxOptions
HttpMethod = "POST",
OnSuccess = "onSuccess ",
OnFailure = "OnFailure",
OnBegin = "Begin()" },
new { // <-- htmlAttributes
@class = "form-horizontal",
id = "error-form" }))

关于javascript - Ajax.BeginForm OnSuccess、onBegin、OnFailure 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48944359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com