HttpPostedFileBase returns null and Request.Files.Count returns zero in MVC

345 Views Asked by At

enter image description hereSaving 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

https://youtu.be/yXQV915WNys

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> 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; }
}
0

There are 0 best solutions below