Hi santosgernan1,
Use HttpPostedFileBase to select file.
Fotr uploading file refer below article.
ASP.Net MVC: Simple File Upload Tutorial with example
Refer below sample.
SQL
CREATE TABLE EmployeeData
(
IDNo INT IDENTITY PRIMARY KEY,
EmpName VARCHAR(50),
Photo VARCHAR(100),
MarkSheet VARCHAR(100)
)
Model
public class EmployeeModel
{
[Required(ErrorMessage = "Please Enter Name.")]
public string EmpName { get; set; }
[Required(ErrorMessage = "Please select Photo.")]
public HttpPostedFileBase Photo { get; set; }
[Required(ErrorMessage = "Please select Marksheet.")]
public HttpPostedFileBase Marksheet { get; set; }
}
Namespaces
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(EmployeeModel model)
{
if (ModelState.IsValid)
{
string photoPath = "~/Files/Photos/";
string marksheetPath = "~/Files/Marksheets/";
string photo = string.Empty;
string marksheet = string.Empty;
if (model.Photo != null)
{
photo = Path.Combine(photoPath, Path.GetFileName(model.Photo.FileName));
model.Photo.SaveAs(Server.MapPath(photo));
}
if (model.Marksheet != null)
{
marksheet = Path.Combine(marksheetPath, Path.GetFileName(model.Marksheet.FileName));
model.Photo.SaveAs(Server.MapPath(marksheet));
}
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
string spName = "INSERT INTO EmployeeData VALUES (@Name, @Photo, @Marksheet)";
using (SqlCommand cmd = new SqlCommand(spName, con))
{
cmd.Parameters.AddWithValue("@Name", model.EmpName);
cmd.Parameters.AddWithValue("@Photo", photo);
cmd.Parameters.AddWithValue("@Marksheet", marksheet);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
return View();
}
return View(model);
}
}
View
@model Sample_237538.Models.EmployeeModel
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
.error { color: red; }
</style>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td><span>Name:</span></td>
<td>@Html.TextBoxFor(m => m.EmpName)</td>
<td>@Html.ValidationMessageFor(m => m.EmpName, "", new { @class = "error" })</td>
</tr>
<tr>
<td><span>Photo:</span></td>
<td>@Html.TextBoxFor(m => m.Photo, new { type = "file" })</td>
<td> @Html.ValidationMessageFor(m => m.Photo, "", new { @class = "error" })</td>
</tr>
<tr>
<td><span>Marksheet:</span></td>
<td>@Html.TextBoxFor(m => m.Marksheet, new { type = "file" })</td>
<td> @Html.ValidationMessageFor(m => m.Marksheet, "", new { @class = "error" })</td>
</tr>
</table>
<br />
<input type="submit" value="Upload" />
}
</body>
</html>
Screenshot
![](https://i.imgur.com/KdJPgKV.png)