1

私のORM(LightSpeed)は、NameとAgeを使用してAnimalsテーブルに対してこれを生成します。MVC3とRazorの使用

   [Serializable]
  [System.CodeDom.Compiler.GeneratedCode("LightSpeedModelGenerator", "1.0.0.0")]
  [System.ComponentModel.DataObject]
  [Table(IdColumnName="AnimalID", IdentityMethod=IdentityMethod.IdentityColumn)]
  public partial class Animal : Entity<int>
  {     
    [ValidatePresence]
    [ValidateLength(0, 50)]
    private string _name;

    [ValidateComparison(ComparisonOperator.GreaterThan, 0)]
    private int _age;

    public const string NameField = "Name";
    public const string AgeField = "Age";

    [System.Diagnostics.DebuggerNonUserCode]
    [Required] // ****I put this in manually to get Name required working
    public string Name
    {
      get { return Get(ref _name, "Name"); }
      set { Set(ref _name, value, "Name"); }
    }

    [System.Diagnostics.DebuggerNonUserCode]
    public int Age
    {
      get { return Get(ref _age, "Age"); }
      set { Set(ref _age, value, "Age"); }
    }

[必須]属性が追加された場合:

ここに画像の説明を入力してください

[必須]属性が追加されていない場合:(検証のLightSpeedの奇妙なレンダリングに注意してください)

ここに画像の説明を入力してください

名前を記入して:

ここに画像の説明を入力してください

上の画像では、上部の検証はLightSpeed(ValidationSummaryに配置)であり、側面の検証はMVC3(ValidationMessageForに配置)です。

現在、サーバー側の検証のみを使用しています。

質問: MVC3でLightSpeed検証をうまく機能させるにはどうすればよいですか?

私はそれがこの分野の何かだと思いますhttp://www.mindscapehq.com/staff/jeremy/index.php/2009/03/aspnet-mvc-part4/

サーバー側の検証では、DefaultModelBinderの動作を利用するのではなく、LightSpeed検証からエラーをより正確に出力するカスタムモデルバインダーを使用する必要があります。MvcのコミュニティコードライブラリからEntityModelBinderを直接使用または適合させる方法をご覧ください

http://www.mindscapehq.com/forums/Thread.aspx?PostID=12051

4

2 に答える 2

1

リンクhttp://www.mindscapehq.com/forums/Thread.aspx?ThreadID=4093を参照してください

ジェレミーズの答え(マインドスケープは素晴らしいサポートをしています!)

public class EntityModelBinder2 : DefaultModelBinder
  {
    public static void Register(Assembly assembly)
    {
      ModelBinders.Binders.Add(typeof(Entity), new EntityModelBinder2());

      foreach (Type type in assembly.GetTypes())
      {
        if (typeof(Entity).IsAssignableFrom(type))
        {
          ModelBinders.Binders.Add(type, new EntityModelBinder2());
        }
      }
    }

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
      object result = base.BindModel(controllerContext, bindingContext);

      if (typeof(Entity).IsAssignableFrom(bindingContext.ModelType))
      {
        Entity entity = (Entity)result;

        if (!entity.IsValid)
        {
          foreach (var state in bindingContext.ModelState.Where(s => s.Value.Errors.Count > 0))
          {
            state.Value.Errors.Clear();
          }

          foreach (var error in entity.Errors)
          {
            if (error.ErrorMessage.EndsWith("is invalid")) continue;
            bindingContext.ModelState.AddModelError(error.PropertyName ?? "Custom", error.ErrorMessage);
          }
        }
      }

      return result;
    }
  }

およびGlobal.asaxレジスタで以下を使用します。

EntityModelBinder2.Register(typeof(MyEntity).Assembly);

Register呼び出しは、モデルアセンブリの各エンティティタイプに使用されるモデルバインダーを設定するため、必要に応じて変更します。

于 2011-03-21T03:34:07.447 に答える
0

2011年4月4日以降、Lightspeedナイトリービルドでクライアント側の検証を機能させることができます。

次のようにバリデータープロバイダーを作成します。

public class LightspeedModelValidatorProvider : DataAnnotationsModelValidatorProvider
{
    private string GetDisplayName(string name)
    {
        return name; //  go whatever processing is required, eg decamelise, replace "_" with " " etc
    }

    protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
    {

        if(typeof(Entity).IsAssignableFrom(metadata.ContainerType))
        {
            List<Attribute> newAttributes = new List<Attribute>(attributes);
            var attr = DataAnnotationBuilder.GetDataAnnotations(metadata.ContainerType, metadata.PropertyName, GetDisplayName(metadata.PropertyName));
            newAttributes.AddRange(attr);

            return base.GetValidators(metadata, context, newAttributes);
        }

        return base.GetValidators(metadata, context, attributes);
    }
}

次に、Application_Start()に追加します

        ModelValidatorProviders.Providers.Clear();
        ModelValidatorProviders.Providers.Add(new LightspeedModelValidatorProvider());
于 2011-04-04T21:27:42.970 に答える