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:
- @model List<DocumentsMvc.Models.DocumentVM>
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport" content="width=device-width" />
- </head>
- <body>
- @using (Html.BeginForm("DocumentSave", "Documents", FormMethod.Post, new { id = "popupForm", enctype = "multipart/form-data" }))
- {
- @Html.AntiForgeryToken()
-
- <h4>@ViewBag.Title</h4>
- <hr />
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
- <fieldset>
- <table>
- <thead>
- <tr>
- <th>Row Number</th>
- <th>Document Name</th>
- <th>Document Date</th>
- <th>Document No</th>
- </tr>
- </thead>
- @if (Model != null && Model.Count > 0)
- {
- int j = 0, s = 0;
- foreach (var i in Model)
- {
- <tr>
- @if (i.RowNumber> 0)
- {
- <td>@Html.DisplayFor(k => k[j].RowNumber, new { htmlAttributes = new { @class = "form-control" } })</td>
- }
- else
- {
- s++;
- <td>@s</td>
- }
-
- <td>@Html.DisplayFor(k => k[j].DocumentName, new { htmlAttributes = new { @class = "form-control" } })</td>
- <td>@Html.EditorFor(k => k[j].DocumentDate, new { htmlAttributes = new { @class = "form-control" } })</td>
- <td>@Html.EditorFor(k => k[j].DocumentNo, new { htmlAttributes = new { @class = "form-control" } })</td>
- <td>@Html.TextBoxFor(k => k[j].UploadDocument, new { type = "file" })</td>
- </tr>
- j++;
- }
- }
- </table>
- <div class="row" style="border:ridge">
- <div class="col-md-offset-2 col-md-4">
- <input type="submit" value="Save" class="btn btn-success" />
- </div>
- </div>
- </fieldset>
- }
- </body>
- </html>
My Control:
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult DocumentSave(List documentVM, HttpPostedFileBase[] UploadDocument)
- {
- var MyFilesCount = Request.Files.Count;
-
- var errors = ModelState.Where(f => f.Value.Errors.Count > 0).Select(s => new { s.Key, s.Value.Errors }).ToArray();
- bool state = false;
- if (ModelState.IsValid)
- {
- if (NewRow == true)
- {
- ...
- }
- db.SaveChanges();
- state = true;
- }
- return new JsonResult { Data = new { state = state } };
- }
My View Model:
- public class DocumentVM
- {
- public int ID { get; set; }
- public int ReasonID { get; set; }
- public int RowNumber { get; set; }
- [DataType(DataType.Date)]
- [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
- public DateTime DocumentDate { get; set; }
- public string DocumentNo { get; set; }
- public HttpPostedFileBase UploadDocument { get; set; }
- }