In this article I will explain with an example, how to pass (send) other Form Data along with File Upload in ASP.Net MVC Razor.
This article will illustrate how to POST (Submit) a Form with some TextBox fields and an HTML FileUpload element using Html.BeginForm helper function in ASP.Net MVC Razor.
Namespaces
You will need to import the following namespace.
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
Action method for handling POST operation
This Action method accepts two parameters, first of type Form Collection i.e. Request.Form collection and second of type HttpPostedFileBase.
The values of both the TextBoxes are fetched using the Form Collection i.e. Request.Form collection using their Name attribute values.
The uploaded File is available in HttpPostedFileBase object. First a check is performed whether Directory (Folder) exists if not then it is created and then the file is saved to it.
Note: The name of the HttpPostedFileBase parameter and the name of HTML FileUpload element must be exact same, otherwise the HttpPostedFileBase parameter will be NULL.
public class HomeController : Controller
{
// GET: Home
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(FormCollection form, HttpPostedFileBase postedFile)
{
string name = form["Name"];
string country = form["Country"];
if (postedFile != null)
{
string path = Server.MapPath("~/Uploads/");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
postedFile.SaveAs(path + Path.GetFileName(postedFile.FileName));
}
return View();
}
}
View
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
There are two TextBoxes and an HTML FileUpload element. The TextBox for the Name value is a simple HTML INPUT element while the TextBox for the Country value is created using Html.TextBox helper function.
There’s also a Submit Button at the end of the Form and when the Button is clicked, the Form is submitted.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table cellpadding="5">
<tr>
<td>Name:</td>
<td><input type="text" name="Name"/></td>
</tr>
<tr>
<td>Country:</td>
<td>@Html.TextBox("Country")</td>
</tr>
<tr>
<td>File:</td>
<td><input type="file" name="postedFile"/></td>
</tr>
</table>
<br/>
<input type="submit" value="Submit"/>
}
</div>
</body>
</html>
Screenshots
Form with two TextBox fields and a FileUpload field
TextBox Values read from Form Collection and the File is read from HttpPostedFileBase object
Downloads