2016-06-11 6 views
0

カスタムモデルバインダーを使用しようとしましたが、request.filesに値が設定されていません。 INフォームのコレクションでは、byte []プロパティの型ファイルの入力にはファイル名のみが入ります。mvc genericly binding byte []プロパティ

<input id="collection[@index][email protected]" name="collection[@index][email protected]" type="file" /> 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace male.services.mvc.Binders 
{ 
    public class CustomModelBinder : DefaultModelBinder 
    { 

    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) 
    { 
     if (bindingContext.ModelType.GetProperties().Any(o => o.PropertyType == typeof(byte[]))) 
     { 
     HttpRequestBase request = controllerContext.HttpContext.Request; 
     foreach (var pi in bindingContext.ModelType.GetProperties().Where(o => o.PropertyType == typeof(byte[]))) 
     { 
      // can't access any property in the parameters that gives me my file input or my stream 
     } 
     return base.BindModel(controllerContext, bindingContext); 
     } 
     else 
     { 
     return base.BindModel(controllerContext, bindingContext); 
     } 
    } 

    } 
} 

答えて

0

私は答えを見つけました。これにより、htmlファイルの入力からEFモデルへ直接、各モデルタイプとそれを必要とするプロパティタイプのファイル引数を手作業で作成せずに行くことができます。 (Aint nobody got time for that

ファイルを投稿するフォームのためには、enctype属性持っている必要があります。それが完了したら、ファイルがcontrollerContext.HttpContext.Request.Files

UPDATE介してアクセスすることができます

<form enctype="multipart/form-data" ... /> 

を - ワーキング例

注:コレクションをバインドしようとしていない場合は、Regexは必要ありません。 EndsWithは不要です。==

関連する問題