Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 DosyaID = Convert.ToString(Kayit.Sira);
                //var Dosya = Request.Files.Count;
                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++;
                //Dosya.SaveAs(DosyaYolu);
                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
Posted
Updated 30-Apr-20 6:54am
v5
Comments
F-ES Sitecore 30-Apr-20 6:16am    
Your code doesn't tie up in any way. The function that accepts the files is DocumentSave, but you're not referencing that anywhere.
Member 14625279 30-Apr-20 11:09am    
I can't understand you. Please, can you give me more details?
Richard Deeming 30-Apr-20 12:05pm    
Your action is called DocumentSave. Your view model is called DocumentVM, which has properties called ID, ReasonID, RowNumber, DocumentDate, DocumentNo, and UploadDocument.

The view you have shown us is posting to an action called BaBeKaydet. Its view model is a list of BorcAlacakMvc.Models.BaBelgeVM objects, which appear to have properties called ID, SebepBelgeID, BorcAlacakID, BelgeSira, BelgeAdi, BelgeTarihi, BelgeNo, Veri, and UBelge. It does not have an HttpPostedFileBase property.

There is no connection between the view you have shown us and the action and viewmodel you have shown us.

Click the green "Improve question" link and update your question to include the real view, action, and viewmodel you are using.
Member 14625279 30-Apr-20 12:37pm    
Sorry. I edit.

1 solution

Quote:
C#
string DosyaYukle = "[" + i + "]UBelge";
HttpPostedFileBase UBelge = Request.Files[DosyaYukle];
You're missing a "." in the name:
C#
string DosyaYukle = "[" + i + "].UBelge";
HttpPostedFileBase UBelge = Request.Files[DosyaYukle];
You're also only ever looking at the first item, since i is always 0.

But you shouldn't need to do it like that; just use the property from the viewmodel:
C#
HttpPostedFileBase UBelge = Kayit.UBelge;
 
Share this answer
 
Comments
Member 14625279 30-Apr-20 13:41pm    
HttpPostedFileBase[] UBelge returns null and Request.Files.Count returns zero.
For example
UBelge{0] null
UBelge[1] null
UBelge[2] null
...
Richard Deeming 30-Apr-20 13:43pm    
If the Request.Files collection is empty, then you haven't selected any files to upload.
Member 14625279 30-Apr-20 13:46pm    
I select files, but it does not transfer from View to Controller. Please watch https://youtu.be/yXQV915WNys

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900