0

I've looked quite a bit online, and every example I see explains how to get IEnumerable<HttpPostedFileBase> into a controller, OR how to get a model, but not both.

What I want is something like:

<form>
    <input type="text" name="Stored file name>
    <input type="file" multiple="multiple" name="files>
    <input type="submit">
</form>

with controller

[ActionName("Index"), HttpPost]
public ActionResult IndexPost(Models.MyModel mdl, IEnumerable<HttpPostedFileBase> files)
{
    // Do some something with the data in mdl and files here
    return RedirectToAction("Index");
}

But every way I try to implement this, it comes back 'no parameterless handler found'. It works fine if I don't include the model.

Am I missing something really obvious?

tereško
  • 58,060
  • 25
  • 98
  • 150
Rollie
  • 4,391
  • 3
  • 33
  • 55
  • Post the definition for `MyModel` (sounds like you have do not have a parameterless constructor for `MyModel`) –  Jul 09 '14 at 05:59
  • 1
    your form element's `name` property must match with your `MyModel` class, your ` – Hiren Kagrana Jul 09 '14 at 06:01

1 Answers1

0

I guess you couldn't find Darin's this post. It's not exactly about passing model with HttpPostedFileBase but if you undrerstand the behavior the it's easy to do that.

See below example.

View

@model WebApplication2.Models.MyModel

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.HiddenFor(m => m.Id)
    <input type="file" name="file" />
    <input type="submit" value="OK" />
}

Action Method

        [HttpPost]
        public ActionResult Index(MyModel model, HttpPostedFileBase file)
        {
            if (file != null)
            {
                //do your stuff here
            }
            return View();
        }
Community
  • 1
  • 1
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
  • I did actually find this - what happened was that I had the issue originally, tried a bunch of fixes, no luck, but I *did* fix my initial problem, but re-broke it with one of the other 'fixes'. Doh :P – Rollie Jul 09 '14 at 15:39