1

モデルバインダーを使用して一部のデータを操作しています。そして、バインドされたモデルを .net から取得して操作したいと考えています。

   public class FilePointerBinder : IModelBinder
    {
        public new object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
              //MyCustomSubModel  value = new MyCustomSubModel();//THIS IS NOT GOOD
              MyCustomSubModel value = (MyCustomSubModel)GetTheDefaultValueFromSomeWhere();
              value.Id = 0;
              return value;
        }
    }

編集

public class User{
   public int Id{get;set;}
   public FilePointer File{get;set;}// this is null
}
4

1 に答える 1

0

次のようなことができます。

public class FilePointerBinder : IModelBinder
{
    public new object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue("FilePointer");
        if(value != null)
        {
            //Object is not null
           return value;
        }
        else
        {
           return new FilePointer();//Filepointer with default value                
        }

    }
}

編集:これが Global.asax.cs にあると仮定すると、Application_Start():

ModelBinders.Binders.Add(typeof(FilePointer), new FilePointerBinder());

BindModel() にブレーク ポイントを設定して、値を監視できます。

于 2013-10-04T21:17:45.460 に答える