我的 asp.net MVC Web 应用程序中有以下 Ajax.BeginForm :-
@using (Ajax.BeginForm("AddZone", "DataCenter",
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.InsertBefore,
UpdateTargetId = "zonetableBody",
LoadingElementId = "progress3",
OnSuccess = "createsuccess",
OnFailure="createfail"
}))
{
这将调用以下操作方法:-
[HttpPost]
[ValidateAntiForgeryToken]
[CheckUserPermissions(Action = "Edit", Model = "Zone")]
public ActionResult AddZone(DataCenterDetails z)
{
在执行操作方法之前,我正在执行自定义授权操作过滤器,如下所示:-
namespace TMS.Controllers
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class CheckUserPermissionsAttribute : ActionFilterAttribute
{
public string Model { get; set; }
public string Action { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
int value = 0;
Repository repository = new Repository();
if (!repository.can(ADusername,Model,value))
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
var viewResult = new PartialViewResult();
viewResult.ViewName = "~/Views/Errors/_Unauthorized.cshtml";
filterContext.Result = viewResult;
我面临的问题是,如果用户没有所需的 Edit 权限,并且他单击 ajax.beginform 按钮,他将收到“/_Unauthorized.cshtml”视图,但同时显示的 Onsuccess 脚本将触发“记录已成功添加”的消息。
所以我的问题是如何防止 Onsuccess 脚本被触发,以防万一_Unauthorized.cshtml
返回?
谢谢