2016-05-01 36 views
0

私はMVCとWebプログラミングが初めてです。イメージのHttpPostedFileBaseをアップロードしますが、常にnullです

私のアップロードイメージはうまくいきますが、ほぼ同じコードを使用してアップロードを変更できるようにすると、HttpPostedFileBaseは常にnullになります。私を夢中にする...

ここにモデルです パブリッククラスModifyGenreViewModel { public int Id {get;セット; }

 [Display(Name = "Nom du style")] 
     public string Name { get; set; } 

     [Display(Name = "Image")] 
     public HttpPostedFileBase Image { get; set; } 
    } 

とビュー

@using (Html.BeginForm("ModifyGenre", "Upload", null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
     { 
      @Html.AntiForgeryToken() 
      <div class="form-horizontal"> 

       <h4>@Resource.StyleCreationHeader</h4> 

       <hr /> 
       @Html.ValidationMessageFor(model=>Model) 
       <div class="form-group" style="display:none"> 

        @Html.LabelFor(model => model.Id, htmlAttributes: new { @class = "control-label col-md-2" }) 
        <div class="col-md-5"> 
         @Html.EditorFor(model => model.Id, new { htmlAttributes = new { @class = "form-control" } }) 
        </div> 
       </div> 

       <div class="form-group"> 

        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" }) 
        <div class="col-md-5"> 
         @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
         @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="form-group"> 
        @Html.LabelFor(model => model.Image, new { @class = "control-label col-md-2" }) 
        <div class="col-md-10"> 
         <input type="file" data-val="true" id="ModifyGenreViewModel_Image" name="ModifyGenreViewModel.Image" /> 
         @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" }) 
        </div> 
       </div> 

       <div class="form-group"> 
        <div class="col-md-offset-2 col-md-10"> 
         <input type="submit" value="@Resource.Modify" class="btn btn-primary" /> 
        </div> 
       </div> 
      </div> 
     } 

私は私のコントローラにブレークポイントを設定すると、私はIDと名前を見ますが、画像は常にnullです。

ありがとうございました!

+0

'@ Html.TextBoxFor(M => m.Image、新しい{タイプ= "ファイル"})'正しいHTML –

答えて

2

ファイル入力の名前プロパティ値は、モデルバインディングが正しく機能するように、ビューモデルプロパティ名と一致する必要があります。あなたのHttpPostアクションメソッドを想定し"Image"

<input type="file" data-val="true" id="ModifyGenreViewModel_Image" name="Image" /> 

変更入力フィールド名をパラメータとしてModifyGenreViewModelオブジェクトを受け入れます。

[HttpPost] 
public ActionResult ModifyGenre(ModifyGenreViewModel model) 
{ 
    // to do : return something 
} 
+1

感謝のあなたの速い答えのために生成されます!それは動作します –

関連する問題