HttpPostedFileBase возвращает ноль, а Request.Files.Count возвращает ноль в MVC - PullRequest
1 голос
/ 29 апреля 2020

enter image description here Сохранение списка документов, созданных в соответствии с выбранной пользователем причиной, в базу данных и их сохранение на сервере.

Например:
Три разных документы по причине «X»
пять различных документов по причине «Y»
шесть различных документов по причине «Z»

https://youtu.be/yXQV915WNys

Мой вид:

@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>

Мой контроль:

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

Мой вид Модель:

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