Hi jsaneelachowd...,
Refer below sample code.
Using the below article i have created the sample.
Controller
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
FilesEntities entities = new FilesEntities();
return View(entities.Files.ToList());
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase postedFile)
{
//Extract Image File Name.
string fileName = System.IO.Path.GetFileName(postedFile.FileName);
//Set the Image File Path.
string filePath = "~/Uploads/" + fileName;
//Save the Image File in Folder.
postedFile.SaveAs(Server.MapPath(filePath));
//Insert the Image File details in Table.
FilesEntities entities = new FilesEntities();
entities.Files.Add(new File
{
Name = fileName,
Path = filePath
});
entities.SaveChanges();
//Redirect to Index Action.
return RedirectToAction("Index");
}
}
View
@model IEnumerable<WebGrid_Image_Path_Database_EF_MVC.File>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<style type="text/css">
body { font-family: Arial; font-size: 10pt; }
</style>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="postedFile" />
<input type="submit" id="btnUpload" value="Upload" />
}
<hr />
<div>
@foreach (var solutionImage in Model)
{
<img src="@Url.Content(solutionImage.Path)" alt="Solution Image">
}
</div>
</body>
</html>
Screenshot