-1

In my MVC application I try to add multiple image insert to my view where I have my ViewModel fields. So I would call my controller with one common button.

@using (Html.BeginForm())
{
    <form action="" method="post" enctype="multipart/form-data">                                  
     <div>
      <input id="fileupload" type="file" multiple="multiple" name="files" />
     </div>
    </form>
     <div>
       .... some textbox etc. (InsertViewModel)
     </div>
    <div class="form-group">
     <div class="col-md-offset-8 col-md-10">
       <input type="submit" value="Create" class="btn btn-primary" />
     </div>
    </div>
}

My controller looks:

 [HttpPost]
public async Task<ActionResult> Insert(InsertViewModel model, IEnumerable<HttpPostedFileBase> files)
 { ..... }

but allways I get count of my files object 0.

Any idea?

Martin
  • 77
  • 2
  • 12
  • Possible duplicate of [MVC 4 Razor File Upload](http://stackoverflow.com/questions/15680629/mvc-4-razor-file-upload) – freedomn-m Oct 27 '16 at 12:53
  • You should create the HTML Form using the `@Html.BeginForm` method – Glenn Ferrie Oct 27 '16 at 12:54
  • Specifically, in this case, `input type=file` is not of type `IEnumerable` so the default MVC binder does not bind to your parameter, leaving it empty. – freedomn-m Oct 27 '16 at 12:57
  • So how can I change `input type=file` to `IEnumerable`? – Martin Oct 27 '16 at 13:12
  • You have nested forms which is invalid html (and the outer form without the `enctype="multipart/form-data"` is being submitted) Remove it! –  Oct 27 '16 at 21:51

1 Answers1

1

please Add submit button into form and all text box also. then it works fine. Code For that is in View

<form action="" method="post" enctype="multipart/form-data">
<div>
    <input id="fileupload" type="file" multiple="multiple" name="files" />
</div>
<div>
    <input id="name" type="text"  name="name" />
</div>
<div>
    <input id="address" type="text" name="address" />
</div>
<div class="form-group">
    <div class="col-md-offset-8 col-md-10">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</div>

Hello

My Controller look like

 [HttpPost]
    public ActionResult Index(IEnumerable<HttpPostedFileBase> files, Demo obj)
    {
        return View();
    }

MultipleUploadFileBinded

ClassObjectBinded

And If You need two form in single view you can also send upload file and receive into controller using Request.Files

 if (Request.Files.Count > 0)//// Is image is uplaod by browse button
 {
               foreach(var fl in Request.Files)
               {
                    var inputStream = Request.Files[0].InputStream;
               }

  }

Like Above you can get stream of file and etc.

Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26
  • This is not addressing OP's issue - the problem is the invalid nested forms! –  Oct 27 '16 at 21:51
  • can have several
    elements in one HTML page, you cannot nest them.
    – Lalji Dhameliya Oct 28 '16 at 04:23
  • Yes, I know. And that's what OP's problem is - they have nested forms (`
    ` is inside another form generated by `@using (Html.BeginForm())`) - all this answer says is to add a submit button which OP already had, and to add textboxes, which OP already had
    –  Oct 28 '16 at 04:26
  • you should either use @using (Html.BeginForm()) or
    element
    – Lalji Dhameliya Oct 28 '16 at 04:32
  • Yes I KNOW!! I'm the one writing the comments. You should edit your question to explain to the OP what the issue is –  Oct 28 '16 at 04:34