Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mvc#,自定义属性在发布错误时导致无限循环_C#_Asp.net Mvc_Custom Attribute - Fatal编程技术网

mvc#,自定义属性在发布错误时导致无限循环

mvc#,自定义属性在发布错误时导致无限循环,c#,asp.net-mvc,custom-attribute,C#,Asp.net Mvc,Custom Attribute,我有这个自定义属性: public class CountAttribute : ValidationAttribute { public string Number1 { get; set; } public string Number2 { get; set; } public CountAttribute() { } public CountAttribute(string Err

我有这个自定义属性:

public class CountAttribute : ValidationAttribute
    {
        public string Number1 { get; set; }
        public string Number2 { get; set; }

        public CountAttribute()
        {

        }

        public CountAttribute(string ErrorMessage, string Number1, string Number2)
        {
            this.Number1 = Number1;
            this.Number2 = Number2;
            this.ErrorMessage = ErrorMessage;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var propertyNumber1 = validationContext.ObjectType.GetProperty(Number1);
            int number1 = (int)propertyNumber1.GetValue(validationContext.ObjectInstance);
            var propertyNumber2 = validationContext.ObjectType.GetProperty(Number2);
            int number2 = (int)propertyNumber2.GetValue(validationContext.ObjectInstance);
            if (number1 + number2 != (int)value)
            {
                return new ValidationResult(ErrorMessage);
            }
            return ValidationResult.Success;
        }
    }
在布局上,我用模态窗口呈现了这样的动作(这将导致无限循环崩溃):


@{Html.RenderAction(“登录”、“帐户”);}
当自定义属性被注释或模型有效时,FormPost工作正常

当我发布带有错误的表单时,它会正确地重新显示模型,但当我再次发布它(有效或无效)时,它会堆叠在循环中。在渲染(登录)上写入此错误:

堆栈不足,无法继续安全执行程序。这个可以 由于调用堆栈上的函数太多或 堆栈使用了太多的堆栈空间

如何解决这个问题

编辑:

        [AllowAnonymous]
        public PartialViewResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return PartialView();
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return PartialView(model);
            }
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

            switch (result)
            {
                case SignInStatus.Success:
                   // some redirect logic
                default:
                    ModelState.AddModelError("", "Invalid login.");
                    return PartialView(model);
            }
        }
[AllowAnonymous]
公共PartialViewResult登录(字符串返回URL)
{
ViewBag.ReturnUrl=返回URL;
返回PartialView();
}
[HttpPost]
[异名]
公共异步任务登录(LoginView模型,字符串返回URL)
{
如果(!ModelState.IsValid)
{
返回局部视图(模型);
}
var result=wait SignInManager.PasswordSignInAsync(model.Email、model.Password、model.RememberMe、shouldllockout:false);
开关(结果)
{
案例标志状态成功:
//一些重定向逻辑
违约:
AddModelError(“,“无效登录”);
返回局部视图(模型);
}
}

它与您的验证属性无关。发布您的控制器方法(可能是按照)是的,我同意您的说法,这没有任何意义,但是如果没有自定义属性,它在应用程序的每个帖子上都可以正常工作。我可以尝试重命名它。我重命名了它,但问题仍然存在。但只有当自定义属性的验证失败时,其他属性才能正常工作。它工作正常,因为
ModelState
有效,并且您不返回视图-同样,它与您的属性无关(暂时关闭客户端验证,并在其他控件中输入无效值,以便返回视图并发生相同的情况)这是真的,但我不明白为什么有些操作即使使用禁用客户端验证也能正常工作,而另一些则不能。这与验证属性无关。发布控制器方法(可能是根据)是的,我同意你的说法,这没有任何意义,但如果没有自定义属性,它在应用程序的每个帖子上都可以正常工作。我可以尝试重命名它。我重命名了它,问题仍然存在。但只有当自定义属性的验证失败时,其他属性才能正常工作。它工作正常,因为
ModelState
是有效的,您不返回视图-同样,它与您的属性无关(暂时关闭客户端验证,并在其他控件中输入无效值,以便返回视图,同样的情况也会发生)这是真的,但我不明白,为什么有些操作即使在禁用客户端验证的情况下也能正常工作,而另一些则不能。
        [AllowAnonymous]
        public PartialViewResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return PartialView();
        }

        [HttpPost]
        [AllowAnonymous]
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return PartialView(model);
            }
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);

            switch (result)
            {
                case SignInStatus.Success:
                   // some redirect logic
                default:
                    ModelState.AddModelError("", "Invalid login.");
                    return PartialView(model);
            }
        }