Yair Nevet
Yair Nevet

Reputation: 13003

Pass extra parameters along with the HttpPostedFileBase object

In my MVC app, I have an upload View with GET and POST actions.

the question is how can I pass extra data to the POST Action along with the HttpPostedFileBase object, e.g., some ID for example.

Upvotes: 1

Views: 5524

Answers (1)

Ryand.Johnson
Ryand.Johnson

Reputation: 1906

You just pass it as an additional parameter

HTML:

<form action="" method="post" enctype="multipart/form-data">
  <input type='text' id='txtId' name='id'/>
  <input type="file" name="file" id="file" />

  <input type="submit" />
</form>

Controller:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file, string id) {

  if (file.ContentLength > 0) {
    var fileName = Path.GetFileName(file.FileName);
    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
    file.SaveAs(path);
  }

  return RedirectToAction("Index");

}

Upvotes: 4

Related Questions