2016-05-01 68 views
0

我是新来的MVC和web编程。上传HttpPostedFileBase图像,但总是空

我的图片上传工作正常,但是当我使用几乎相同的代码,允许用户修改它的上传,它HttpPostedFileBase始终为空。让我疯狂......

这里是模型 公共类ModifyGenreViewModel {int}公共int Id {获取;组; }

 [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和姓名,但图像始终为空。

感谢的对你有所帮助!您的文件输入

+0

'@ Html.TextBoxFor(M => m.Image,新{TYPE = “文件”})'会产生正确的HTML –

回答

2

name属性值应该匹配与模型绑定正常工作视图模型属性名。

改变输入字段名称以"Image"

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

假设你HttpPost动作方法接受ModifyGenreViewModel对象作为参数。

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

感谢对你快回答!它确实有效 –

相关问题