'Why images paths not store in database MVC5?
I'm new to MVC so I hope to get help regarding my question. I am making users upload images. the image path will store in the database but the image itself will store in the Content folder. The code is working fine (which means there is no error) but I don't get the wanted result. I appreciate your help. Thanks in advance.
Here is the Controller code:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "EmployeeId,EmpName,EmpEmail,Poition,Nationality,Last_W_D,EmpHOD,Password,DepId,SignaturePath,DoctorCategory")] Tbl_Employee tbl_Employee, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if(file != null)
{
file.SaveAs(HttpContext.Server.MapPath("~/Content/Images/Employees/") + file.FileName);
tbl_Employee.SignaturePath = file.FileName;
}
db.Tbl_Employee.Add(tbl_Employee);
db.SaveChanges();
return RedirectToAction("Index");
}
Here is the "Create view " code:
@model ClearanceFirst.Models.Tbl_Employee
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Tbl_Employee", null, FormMethod.Post, new { enctype = "
multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Tbl_Employee</h4>
<hr />
<div class="form-group">
@Html.LabelFor(model => model.SignaturePath, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input id="signaturePath " title ="upload images " type="file" name="file" />
@Html.ValidationMessageFor(model => model.SignaturePath, "", new { @class = "text-danger" })
</div>
</div>
Here is the "Index view" code:
<td>
@if (!string.IsNullOrWhiteSpace(item.SignaturePath))
{
<img width = "70" height = "50"
src="~/Content/Images/Employees/@Url.Content(item.SignaturePath)"
alt= "Alternate Text"/> }
</td>
Solution 1:[1]
The uploaded Images will be converted to Binary format then saved to SQL Server Database table.
Check the below given links for more information, how to upload and retrieve Images from Sql Server in ASP.NET MVC,
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | TechieWords |