Hi @Abeer S ,
I tried your code and found the problem, your file is always empty, so the code doesn't execute the statement in the if.
I wrote a demo according to your needs, you can refer to it.
Controller
public class HomeController : Controller
{
private EmpContext db = new EmpContext();
public ActionResult Index()
{
return View();
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Tbl_Employee tbl_Employee)
{
if (ModelState.IsValid)
{
if (Request.Files.Count > 0)
{
HttpPostedFileBase file = Request.Files[0];
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
tbl_Employee.SignaturePath = Path.Combine(
Server.MapPath("~/Upload"), fileName);
file.SaveAs(tbl_Employee.SignaturePath);
}
db.Tbl_Employee.Add(tbl_Employee);
db.SaveChanges();
return RedirectToAction("Index");
}
}
return View(tbl_Employee);
}
}
cshtml
@model ContosoUniversity.Models.Tbl_Employee
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="editor-field">
@Html.TextBoxFor(model => model.SignaturePath, new { type = "file" })
@Html.ValidationMessageFor(model => model.SignaturePath)
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
}
Best regards,
Lan Huang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.