2013-04-30 49 views
0

出于某种原因,我的表单发布不起作用,当我在mvc3剃须刀的隐藏领域。它工作正常,如果我删除隐藏的领域,但我需要该领域。表单发布不工作,由于@HiddenFor在mvc3

下面是我的ProductsController post方法和Razor视图

@model CCSPurchasing_MVC.Models.AddNewProductModel 
    @using CCSPurchasing_MVC.Models 
@using (Html.BeginForm("editImage", "Products", FormMethod.Post)) 
{ 
@Html.ValidationSummary(true) 

     @Html.HiddenFor(m => m.ImadeId) 


    <div class="editor-field"> 
    <p style="margin-left: 300px; margin-right: 20px;"> 
      @Html.LabelFor(m => m.ImageFile) 


      <input type="file" name="file" id="file" data-val="true" data-val-required="Product Image is required"/> 


    </p> 
    </div> 

    <input type="submit" value="Edit" /> 



} 



[HttpPost] 
public ActionResult editImage(AddNewProductModel newP, HttpPostedFileBase file) 
{ 


     db = new DBConnection(); 
     if (file != null && file.ContentLength > 0) 
     { 

      newP.ImageName = Path.GetFileName(file.FileName); 
      newP.ImageType = file.ContentType; 
      newP.ImageSize = file.ContentLength; 
      newP.ImageFile = new byte[file.ContentLength]; 
      file.InputStream.Read(newP.ImageFile, 0, file.ContentLength); 
      newP.ImageHeight = 200; 
      newP.ImageWidth = 200; 

     } 
     string result = db.editImage(newP);  

    return View("editImageUpdate"); 
} 
+0

请分享你的模型 – 2013-04-30 17:36:33

+0

公共类AddNewProductModel { [必需] [显示(名称= “上传图片”) 公共字节[] {镜像文件获得;组; } public int ImadeId {get;组; } } – user2040259 2013-04-30 17:55:27

+0

Plz在您的问题中添加完整的模型(不作为注释)。 – Sharun 2013-05-01 03:36:51

回答

1

只是让你的表单标签是这样,我相信它会为你工作还因为它为我工作的时候我测试了你的代码:

@using (Html.BeginForm("EditImage", "Home", FormMethod.Post, new { enctype = "multipart/form-data"  })) 
{ 
} 

你需要添加enctype =“multipart/form-data”al所以在你的代码中如果你想用你的文件上传控件提交文件。