2

I want to upload multiple files in list of dynamic form. My model is like

class mymodel
{
    public IEnumerable<FileAttachmentViewModel> FileAttachments { get; set; }
}

public class FileAttachmentViewModel
{
    [DataType(DataType.Upload)]
    public IEnumerable<HttpPostedFileBase> files { get; set; }
    public bool IsPrivate { get; set; }
    public string Description { get; set; }
}

but my controller returns null for files

my view

<div class="panel panel-default">
    <div class="panel-heading">
        File 1
        <button type="button" style="margin-bottom:10px;" class='pull-right btn btn-success add-file'><span class="glyphicon glyphicon-plus"></span>Add File </button>
    </div>
    <div class="panel-body">
        <div class="form-group">
            <label class="label-control col-md-3"> Select File </label>
            <input type="file" class="form-control col-md-4" name="FileAttachments[0][files]" multiple="multiple" />
            <label class="label-control col-md-2"> IsPrivate </label>
            <input type="checkbox" name="FileAttachments[0][IsPrivate]" class="col-md-3" value="true">
        </div>
        <div class="form-group">
            <label class="label-control col-md-3"> Description </label>
            <textarea name="FileAttachments[0][Description]" class="form-control col-md-9"></textarea>
        </div>
    </div>
</div>
<div id="fileupload"></div>

its a dynamic view with javascript. and my controller

public ActionResut Register(myviewmodel model)
{
    if (model.FileAttachments != null)
    {
        foreach (var att in model.FileAttachments)
        {
            if (att.files != null)
            {
                foreach (HttpPostedFileBase file in att.file)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    attachment = new Attachment();
                    attachment.AttachmentId = Guid.NewGuid();
                    attachment.Description = att.Description;
                    attachment.URL = "~/Content/Heritage/Attachments/" + fileName;
                    attachment.Type = file.ContentType;
                    attachment.HeritageId = heritage.HeritageId;
                    //attachment.OtherType = model.OtherType;
                    attachment.IsPrivate = att.IsPrivate;
                    _AttachmentService.Insert(attachment);
                    file.SaveAs(Path.Combine(Server.MapPath("~/Content/Heritage/Attachments/" + fileName)));
                }
            }
        }
    }
}
Sinte
  • 91
  • 1
  • 10
  • 1
    Can you show us the controller code? – mjwills Aug 26 '17 at 12:21
  • Also it might be helpful to show your `view` – Aleks Andreev Aug 26 '17 at 12:29
  • it workes if my model is type of IEnumerable files but my view have dynami form so my viewmoel becomes list of list of IEnumerable files – Sinte Aug 26 '17 at 12:56
  • 1
    Your not generating your form controls correctly - your name attributes would need to be `name="FileAttachments[0].files"` etc. Always use the strongly typed `HtmlHelper` methods to generate the correct html, and to give you 2-way models binding, validation etc. Since your property is a collection, you use a `for` loop or `EditorTemplate` - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Aug 26 '17 at 12:57
  • i know stephen but all other fileds worked for me just file uploading becomes challenge for me – Sinte Aug 26 '17 at 13:03
  • The other fields cannot possible work despite what your claiming , not to mention all the other bad practice in your view (either that or you have shown the wrong code) –  Aug 26 '17 at 13:09
  • owww you are a genius Stephen it worked and its a magic thank you bro. you saved my ass !!! – Sinte Aug 26 '17 at 13:17

0 Answers0