我有以下操作过滤器类,它在调用任何操作方法之前执行自定义授权检查: -
public class CheckUserPermissionsAttribute : ActionFilterAttribute
{
//code goes here
if (!repository.can(ADusername,Model,value ))
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
var viewResult = new JsonResult();
viewResult.Data = (new { IsSuccess = "Unauthorized", description = "You are not authorized to perform this Action." });
filterContext.Result = viewResult;}
}
base.OnActionExecuting(filterContext);}}}
我有以下ajax.beginform
@using (Ajax.BeginForm("AddZone", "DataCenter",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.InsertBefore,
UpdateTargetId = "zonetableBody",
LoadingElementId = "progress3",
OnSuccess = "createsuccess",
OnFailure="createfail"
}))
OnSuccess脚本是: -
function createsuccess(data) {
if (data.IsSuccess == " Unauthorized") {
jAlert(data.description, 'Unauthrozed Access');
}
if (data.IsSuccess == "False") {
jAlert('Error Occured.' + data.description);
}
else {
jAlert('Record was added Successfully ', 'Creation Confirmation');
}
}
所以我认为如果用户点击Ajax.Beginform并且他没有所需的权限,那么actionfilter将返回一个Json,它将被传递给OnSuccess脚本,脚本将显示未经授权的消息。但目前的情况是jAlert('Record was added Successfully ', 'Creation Confirmation');
将显示。那么任何人都可以建议我如何将Json从动作过滤器传递给Onsuccess脚本?
感谢
答案 0 :(得分:2)
删除空格女巫是“未经授权”的第一个字符,并在第一个else
语句后使用if
语句,如下所示
function createsuccess(data) {
if (data.IsSuccess == "Unauthorized") {
jAlert(data.description, 'Unauthrozed Access');
}else
if (data.IsSuccess == "False") {
jAlert('Error Occured.' + data.description);
}
else {
jAlert('Record was added Successfully ', 'Creation Confirmation');
}
}
答案 1 :(得分:0)
摆脱:OnSuccess =“createsuccess”,
在局部视图中传递一个脚本,该脚本在渲染后会被触发。它可以携带在控制器上创建并传递给视图的json对象。
当触发该函数时,它应该点击“createsuccess”并在必要时将json对象传递给它。
<script type="text/javascript">
$(document).ready(function () {
data = yourjsonobject;
createsuccess(data);
});
</script>
“yourjsonobject”将在初始化时在partialview上动态创建,或者您可以在控制器上创建它并将其传递给partialview。