2016-04-07 4 views
0

Umbraco SurfaceControllerでフォームをポストバック中にモデルを検証すると、ModelState.AddModelErrorを使用してユーザーにフィードバックを提供するための検証エラーメッセージを追加できません。なぜどんなアイデア?UmbracoはSurfaceControllerのHttpPost中にAddModelErrorを実行できません

ModelState.AddModelError[ChildActionOnly]レンダリング方法で問題なく使用できます。

[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をコード化しようとすることができますが、私が必要とする検証は他のモデルプロパティに基づいてデータを参照する必要がありますので少し複雑になり始めます。

+0

私はUmbracoフォーラムにこれをクロス投稿しました.Umbraco開発者がスタックオーバーフローの代わりにもっとアクティブになっている場合に備えて - https://our.umbraco.org/forum/templates-partial-views-and-macros/76443- httpsost-in-surfacecontrollerの間にaddmodelerrorはできません – Gavin

答えて

0

私は問題を回避するためのコーディングカスタムSystem.ComponentModel.DataAnnotations.ValidationAttributeに頼っ:speculation that this scenario could be a bug in Umbraco 7.4.2がありますしかし、周りのこの作品は、将来的に必要とされないことも

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 
    } 
} 

関連する問題