2012-07-17 76 views
0

當我用下面的代碼在控制器HttpPost不MVC3工作

[HttpPost] 
public ActionResult Index(HttpPostedFileBase file) 
{ 
    string path = Path.Combine(Server.MapPath("~/Images"), 
           Path.GetFileName(file.FileName)); 
    file.SaveAs(path); 

    ViewBag.Message = "File Uploaded Successfully"; 

    return View(); 
} 

當我運行我得到了以下錯誤

---Server Error in '/' Application. 
---The resource cannot be found. 

應用程序當我刪除[HttpPost]它加載,但文件沒有上傳...

任何人都可以幫助我嗎?......

+1

您是否可以使用用於從視圖中調用操作的代碼/表單來更新您的問題? – nemesv 2012-07-17 08:00:06

+1

控制器中是否有另一個'Index'方法具有屬性'[HttpGet]'? – 2012-07-17 08:04:00

回答

0

Make sur e您正在創建如下形式,

@using (Html.BeginForm("Index", "Home", 
     FormMethod.Post,     
     new { enctype = "multipart/form-data" })) 
{ 
    <input type="file" name="file" /> 
    <input type="submit" value="OK" /> 
}