“return json(true,jsonrequestbehavior.allowget)"在屏幕上返回'true'而不是调用onsuccess方法 [英] "return json(true,jsonrequestbehavior.allowget)" returns 'true' on screen instead of calling onsuccess method
问题描述
"return Json(true,JsonRequestBehavior.AllowGet)" returns 'true' on screen instead of calling Onsuccess Method
我从Ajax.BeginForm调用Action方法(ActionMethod,新的AjaxOptions {OnSuccess =MyScript,HTTPMethod =POST,OnFailure =MyScript2})
我尝试过:
我尝试将Json作为真正的首先它没有工作,所以我尝试返回bool变量本身。唉!它没有。
I am calling Action method from Ajax.BeginForm("ActionMethod",new AjaxOptions{OnSuccess="MyScript",HTTPMethod="POST",OnFailure="MyScript2"})
What I have tried:
I tried returning Json as true first it didnt work so i tried returning the bool variable itself. Alas! it didnt.
推荐答案
按照以下步骤在MVC中实现AJAX.BeginForm
Step1 :在web.config中启用Unobustrive
Follow below steps to implement AJAX.BeginForm in MVC
Step1: Enable Unobustrive in web.config
<appsettings>
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appsettings>
Step2 :在视图页面中添加以下代码(* .cshtml)
Step2: Add following code in your view page(*.cshtml)
@using (Ajax.BeginForm("PerformAction", new AjaxOptions { OnSuccess = "OnSuccess", HttpMethod="POST", OnFailure = "OnFailure" }))
{
<fieldset>
<input type="submit" value="Submit" />
</fieldset>
}
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript">
function OnSuccess(response) {
alert(response);
}
function OnFailure(response) {
alert("Something wrong happens. Error is: " + response);
}
</script>
Step3 :添加在Controller的动作中跟随代码
Step3: Add following code in Controller's action
[HttpPost]
public JsonResult performAction()
{
return Json(true, JsonRequestBehavior.AllowGet);
}
这篇关于“return json(true,jsonrequestbehavior.allowget)"在屏幕上返回'true'而不是调用onsuccess方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!