Umbraco 无法在 SurfaceController 中的 HttpPost 期间添加模型错误
Umbraco cannot AddModelError during HttpPost in SurfaceController
在表单 post 返回期间验证 Umbraco SurfaceController
中的模型时,我无法使用 ModelState.AddModelError
添加验证错误消息以向用户提供反馈。有什么想法吗?
我可以在 [ChildActionOnly]
渲染方法中使用 ModelState.AddModelError
没有问题。
[ChildActionOnly]
public ActionResult VerifyEmail(VerifyEmailModel model)
{
// This DOES work
ModelState.AddModelError("SomeProperty", "Some error message to display.");
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult VerifyEmailSubmit(VerifyEmailModel model)
{
// This DOESN'T work
ModelState.AddModelError("SomeProperty", "Some error message to display.");
return CurrentUmbracoPage();
}
有什么解决这个问题的想法吗?
我想我可以尝试编写自定义代码 System.ComponentModel.DataAnnotations.ValidationAttribute
但我需要做的验证需要根据其他模型属性查找数据,因此开始变得有点复杂。
我使用自定义编码 System.ComponentModel.DataAnnotations.ValidationAttribute
来解决这个问题:
public class VerificationCodeAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string code = (string)value;
Guid userId = Guid.Empty;
PropertyInfo userIdProperty = validationContext.ObjectType.GetProperty("UserId");
if (userIdProperty != null)
userId = (Guid)userIdProperty.GetValue(validationContext.ObjectInstance);
// See if we're confirming email via link
if (userId == Guid.Empty)
userId = MyProject.Identity.Client.GetUser().Id;
if (!string.IsNullOrWhiteSpace(code) && !MyProject.Identity.Client.VerifyUserEmail(userId, code))
return new ValidationResult("Invalid email verification code.");
return null; // Default for empty string
}
}
但是,由于存在 speculation that this scenario could be a bug in Umbraco 7.4.2。
,将来可能不需要此解决方法
在表单 post 返回期间验证 Umbraco SurfaceController
中的模型时,我无法使用 ModelState.AddModelError
添加验证错误消息以向用户提供反馈。有什么想法吗?
我可以在 [ChildActionOnly]
渲染方法中使用 ModelState.AddModelError
没有问题。
[ChildActionOnly]
public ActionResult VerifyEmail(VerifyEmailModel model)
{
// This DOES work
ModelState.AddModelError("SomeProperty", "Some error message to display.");
return View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult VerifyEmailSubmit(VerifyEmailModel model)
{
// This DOESN'T work
ModelState.AddModelError("SomeProperty", "Some error message to display.");
return CurrentUmbracoPage();
}
有什么解决这个问题的想法吗?
我想我可以尝试编写自定义代码 System.ComponentModel.DataAnnotations.ValidationAttribute
但我需要做的验证需要根据其他模型属性查找数据,因此开始变得有点复杂。
我使用自定义编码 System.ComponentModel.DataAnnotations.ValidationAttribute
来解决这个问题:
public class VerificationCodeAttribute : ValidationAttribute
{
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
string code = (string)value;
Guid userId = Guid.Empty;
PropertyInfo userIdProperty = validationContext.ObjectType.GetProperty("UserId");
if (userIdProperty != null)
userId = (Guid)userIdProperty.GetValue(validationContext.ObjectInstance);
// See if we're confirming email via link
if (userId == Guid.Empty)
userId = MyProject.Identity.Client.GetUser().Id;
if (!string.IsNullOrWhiteSpace(code) && !MyProject.Identity.Client.VerifyUserEmail(userId, code))
return new ValidationResult("Invalid email verification code.");
return null; // Default for empty string
}
}
但是,由于存在 speculation that this scenario could be a bug in Umbraco 7.4.2。
,将来可能不需要此解决方法