2
Answers

File Upload Problem in MVC

Saving the list of documents created according to the reason selected by the user to the database and storing them on the server.

For example:
Three different documents for the 'X' reason
Five different documents for the 'Y' reason
Six different documents for the 'Z' reason 

I can create the list but i can't upload files. I have a problem that I can't understand.
 
It's a problem:  
HttpPostedFileBase returns null and Request.Files.Count returns zero.
 
My View:
  1. @model List<DocumentsMvc.Models.DocumentVM>    
  2. @{    
  3.     Layout = null;    
  4. }    
  5. <!DOCTYPE html>    
  6. <html>    
  7. <head>    
  8.     <meta name="viewport" content="width=device-width" />    
  9. </head>    
  10. <body>    
  11.     @using (Html.BeginForm("DocumentSave""Documents", FormMethod.Post, new { id = "popupForm", enctype = "multipart/form-data" }))    
  12.     {    
  13.         @Html.AntiForgeryToken()    
  14.     
  15.         <h4>@ViewBag.Title</h4>    
  16.         <hr />    
  17.         @Html.ValidationSummary(true""new { @class = "text-danger" })    
  18.         <fieldset>    
  19.             <table>    
  20.                 <thead>    
  21.                     <tr>    
  22.                         <th>Row Number</th>    
  23.                         <th>Document Name</th>    
  24.                         <th>Document Date</th>    
  25.                         <th>Document No</th>    
  26.                     </tr>    
  27.                 </thead>    
  28.                 @if (Model != null && Model.Count > 0)    
  29.                 {    
  30.                     int j = 0, s = 0;    
  31.                     foreach (var i in Model)    
  32.                     {    
  33.                         <tr>    
  34.                             @if (i.RowNumber> 0)    
  35.                             {    
  36.                                 <td>@Html.DisplayFor(k => k[j].RowNumber, new { htmlAttributes = new { @class = "form-control" } })</td>    
  37.                             }    
  38.                             else    
  39.                             {    
  40.                                 s++;    
  41.                                 <td>@s</td>    
  42.                             }    
  43.     
  44.                             <td>@Html.DisplayFor(k => k[j].DocumentName, new { htmlAttributes = new { @class = "form-control" } })</td>    
  45.                             <td>@Html.EditorFor(k => k[j].DocumentDate, new { htmlAttributes = new { @class = "form-control" } })</td>    
  46.                             <td>@Html.EditorFor(k => k[j].DocumentNo, new { htmlAttributes = new { @class = "form-control" } })</td>    
  47.                             <td>@Html.TextBoxFor(k => k[j].UploadDocument, new { type = "file" })</td>    
  48.                         </tr>    
  49.                         j++;    
  50.                     }    
  51.                 }    
  52.             </table>    
  53.             <div class="row" style="border:ridge">    
  54.                 <div class="col-md-offset-2 col-md-4">    
  55.                     <input type="submit" value="Save" class="btn btn-success" />    
  56.                 </div>    
  57.             </div>    
  58.         </fieldset>    
  59.     }    
  60. </body>    
  61. </html>  
My Control:
  1. [HttpPost]  
  2. [ValidateAntiForgeryToken]  
  3. public ActionResult DocumentSave(List documentVM, HttpPostedFileBase[] UploadDocument)  
  4. {  
  5.     var MyFilesCount = Request.Files.Count;  
  6.   
  7.     var errors = ModelState.Where(f => f.Value.Errors.Count > 0).Select(s => new { s.Key, s.Value.Errors }).ToArray();  
  8.     bool state = false;  
  9.     if (ModelState.IsValid)  
  10.     {  
  11.         if (NewRow == true)  
  12.         {  
  13.            ...        
  14.         }  
  15.         db.SaveChanges();  
  16.         state = true;  
  17.     }  
  18.     return new JsonResult { Data = new { state = state } };  
  19. }  
My View Model:
  1. public class DocumentVM  
  2. {  
  3.     public int ID { getset; }  
  4.     public int ReasonID { getset; }  
  5.     public int RowNumber { getset; }  
  6.     [DataType(DataType.Date)]  
  7.     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]  
  8.     public DateTime DocumentDate { getset; }  
  9.     public string DocumentNo { getset; }              
  10.     public HttpPostedFileBase UploadDocument { getset; }  
  11. }

Answers (2)