我无法弄清楚为什么Breeze.js (0.84.3)不会向客户端返回由在类级别应用于服务器模型的自定义验证属性设置的错误消息。我可以使用 Breeze TODO KO 样本重现这一点,如下所示:
using System;
using System.ComponentModel.DataAnnotations;
namespace Todo.Models
{
[MyCustomValidator] // NEW
public class TodoItem
{
public int Id { get; set; }
[Required, StringLength(maximumLength: 30)]
public string Description { get; set; } // Set to 'error' to trigger server error.
public System.DateTime CreatedAt { get; set; }
public bool IsDone { get; set; }
public bool IsArchived { get; set; }
}
[AttributeUsage(AttributeTargets.Class)] // NEW
public class MyCustomValidator : ValidationAttribute
{
public override Boolean IsValid(Object value)
{
var todo = value as TodoItem;
if (todo.Description == "error")
{
ErrorMessage = "The TodoItem is not valid!";
return false;
}
return true;
}
}
}
我希望错误消息“TodoItem 无效!” 将返回给客户端,但 Breeze 似乎总是从服务器返回“值不能为空。参数名称:源”。似乎有一个例外发生在:
StackTrace:
at System.Linq.Enumerable.Select[TSource,TResult](IEnumerable`1 source, Func`2 selector) at Breeze.WebApi.EFContextProvider`1.SaveChangesCore(Dictionary`2 saveMap)
at Breeze.WebApi.ContextProvider.SaveChanges(JObject saveBundle)
at Todo.Controllers.TodosController.SaveChanges(JObject saveBundle) in c:\Users\RichardH\Downloads\Software\Web\breeze-runtime-plus-0.84.3\Samples\Todo\Todo\Controllers\TodosController.cs:line 41
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)
谁能阐明我如何将服务器上的验证应用于整个模型(而不仅仅是单个属性/字段)?
谢谢,理查德