2012-06-13 5 views
1

이 코드를 실행할 때 null 개체 참조 예외가 있습니다. 나는 파일 HTML 컨트롤을 내 면도기 코드는 다음과 같습니다MVC HttpPost not working

[HttpPost] 
    public ActionResult AddFiles(HttpPostedFileBase file, int catid, string description) 
    { 
     // Verify that the user selected a file { 
     if (file != null && file.ContentLength > 0) 
     { 
      // extract only the filename 
      var filename = Path.GetFileName(file.FileName); 
      // store the file inside ~/App_Data/Uploads folder 
      var path = Path.Combine(Server.MapPath("~/App_Data/Uploads"), filename); 
      file.SaveAs(path); 
     } 
     RefDataLinks_mst fileDetails = new RefDataLinks_mst() 
     { 
      LastModified = new DateTime(2012,1,1), 
      CategoryID = catid, 
      DataFileName = Path.GetFileName(file.FileName), 
      DataTitle = "SAMPLETITLE", 
      DataID = ((new Random()).Next(100, 1000)), 
      DataFilePath = "Sample/Path", 
      Description = description, 
      UpdatedBy = "Arjel", 
      FileSize = file.ContentLength 
     }; 
     bool b = ServiceReferenceHelper.AddFile(fileDetails); 
     // Redirect back to the index action to show the form once again 
     return Redirect("AddFiles?catid=" + catid); //both works 
     //return RedirectToAction("ViewDataFiles", new { catid = catid }); 
    } 

CATID과 설명이 수신의 데이터 만 null 객체에 대한 파일 참조 :

@using (Html.BeginForm("AddFiles", "AdministerFiles", FormMethod.Post)) 
{ 
    <p><label for="filename">Filename : </label><input type="text" name="filename" /></p> 
    <p><label for="description">Description : </label><input type="text" name="description" /></p> 
    <p><input type="file" name="file" id="file" /> </p> 
    <p><input type="hidden" value="@Model[2]" name="catid"/></p> 

    <input type="submit" value="Upload Now!" /> 
    <input type="reset" value="Reset" /> 
}  

내 AdministerFilesController이있다. 무엇이 문제 일 수 있습니까?

답변

4

실제로 업로드하려면 양식에 enctype 속성을 지정해야합니다. 수행 방법의 예는 다음과 같습니다.

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