2013-04-30 45 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所以在你的代碼中如果你想用你的文件上傳控件提交文件。