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.
- @model List
- @{
- Layout = null;
- }
- "viewport" content="width=device-width" />
- @using (Html.BeginForm("DocumentSave", "Documents", FormMethod.Post, new { id = "popupForm", enctype = "multipart/form-data" }))
- {
- @Html.AntiForgeryToken()
-
@ViewBag.Title
-
- @Html.ValidationSummary(true, "", new { @class = "text-danger" })
-
Row Number Document Name Document Date Document No - @if (Model != null && Model.Count > 0)
- {
- int j = 0, s = 0;
- foreach (var i in Model)
- {
- @if (i.RowNumber> 0)
- {
@Html.DisplayFor(k => k[j].RowNumber, new { htmlAttributes = new { @class = "form-control" } }) - }
- else
- {
- s++;
@s - }
@Html.DisplayFor(k => k[j].DocumentName, new { htmlAttributes = new { @class = "form-control" } }) @Html.EditorFor(k => k[j].DocumentDate, new { htmlAttributes = new { @class = "form-control" } }) @Html.EditorFor(k => k[j].DocumentNo, new { htmlAttributes = new { @class = "form-control" } }) @Html.TextBoxFor(k => k[j].UploadDocument, new { type = "file" }) - j++;
- }
- }
- class="row" style="border:ridge">class="col-md-offset-2 col-md-4">
- "submit" value="Save" class="btn btn-success" />
- }
- [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 } };
- }
- 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; }
- }
Rajanikant HawaldarPosted Apr 30, 2020, 12:14 AM
Jignesh KumarPosted Apr 29, 2020, 10:05 PM
https://forums.asp.net/t/2137543.aspx?Upload+file+through+ajax+mvc+returns+null