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<BorcAlacakMvc.Models.BaBelgeVM>
@{
ViewBag.Title = "";
Layout = null;
}
@using (Html.BeginForm("BaBelgeKaydet", "BorcAlacak", FormMethod.Post, new { id = "popupForm", enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<h4>@ViewBag.Title</h4>
<table>
<thead>
<tr>
<th>Sıra</th>
<th>Belge</th>
<th>Belge Tarihi</th>
<th>Belge No</th>
@*<th>Veri</th>*@
@*<th>class="fa fa-search"></th>*@
</tr>
</thead>
<tbody>
@if (Model != null && Model.Count > 0)
{
int j = 0, s = 0;
foreach (var i in Model)
{
string dosya = "dosya" + j.ToString();
<tr>
@Html.HiddenFor(k => k[j].ID)
@Html.HiddenFor(k => k[j].SebepBelgeID)
@Html.HiddenFor(k => k[j].BorcAlacakID)
@if (i.BelgeSira > 0)
{
<td>@Html.DisplayFor(k => k[j].BelgeSira, new { htmlAttributes = new { @class = "form-control" } })</td>
}
else
{
s++;
<td>@Html.DisplayFor(k => k[j].BelgeSira, new { value = s})</td>
}
<td>@Html.DisplayFor(k => k[j].BelgeAdi, new { htmlAttributes = new { @class = "form-control" } })</td>
<td>@Html.EditorFor(k => k[j].BelgeTarihi, new { htmlAttributes = new { @class = "form-control" } })</td>
<td>@Html.EditorFor(k => k[j].BelgeNo, new { htmlAttributes = new { @class = "form-control" } })</td>
<td>@Html.HiddenFor(k => k[j].Veri, new { htmlAttributes = new { @class = "form-control" } })</td>
<td>@Html.TextBoxFor(k => k[j].UBelge, new { type = "file" })</td>
@*<td><input type="file" id="@j" name="dosya" /></td>*@
</tr>
j++;
}
}
</tbody>
</table>
<div class="row" style="border:ridge">
<div class="col-md-offset-2 col-md-4">
<input type="submit" value="Kaydet" class="btn btn-success" />
</div>
</div>
<div class="err">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
</div>
}
My Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult BaBelgeKaydet(List<BaBelgeVM> baBelgeVM, HttpPostedFileBase[] UBelge)
{
var Dosya = Request.Files.Count;
var errors = ModelState.Where(f => f.Value.Errors.Count > 0).Select(s => new { s.Key, s.Value.Errors }).ToArray();
bool durum = false;
if (ModelState.IsValid)
{
if (YeniKayit == true)
{
foreach (var Kayit in baBelgeVM)
{
int i = 0;
string DosyaYukle = "[" + i + "]UBelge";
HttpPostedFileBase UBelge = Request.Files[DosyaYukle];
var DosyaYolu = Path.Combine(Server.MapPath("~App_Data/Upload"), borcalacakid.ToString());
BorcAlacakBelge borcAlacakBelge1 = new BorcAlacakBelge()
{
Sira = Kayit.BelgeSira,
BorcAlacakID = borcalacakid,
SebepBelgeID = Kayit.SebepBelgeID,
BelgeTarihi = Kayit.BelgeTarihi,
BelgeNo = Kayit.BelgeNo,
Veri = DosyaYolu
};
i++;
db.BorcAlacakBelge.Add(borcAlacakBelge1);
db.SaveChanges();
}
}
db.SaveChanges();
durum = true;
}
return new JsonResult { Data = new { durum = durum } };
}
My View Model:
public class BaBelgeVM
{
public int ID { get; set; }
public int BelgeSira { get; set; }
public int BorcAlacakID { get; set; }
public int SebepBelgeID { get; set; }
public string SebepAdi { get; set; }
public int SebepID { get; set; }
public string BelgeAdi { get; set; }
public int BelgeID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime BelgeTarihi { get; set; }
public string BelgeNo { get; set; }
public string Veri { get; set; }
public HttpPostedFileBase UBelge { get; set; }
public List<BorcAlacakBelge> borcAlacakBelge { get; set; }
public List<BelgeListesi> belgeListesi { get; set; }
}
What I have tried:
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