When i am trying to update the file path, it will stored in database like
System.Web.HttpPostedFileWrapper
. Anyone please give a solution for this in ASP.NET MVC
What I have tried:
Conttroller :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditCandidate(Candidate _candidate, HttpPostedFileBase _resume)
{
if (ModelState.IsValid)
{
var model = _db.Candidates.Find(_candidate.CandidateId);
string oldResumePath = model.ResumePath;
if(_resume != null && _resume.ContentLength > 0)
{
var ResumeName = Path.GetFileName(_resume.FileName);
string path = System.IO.Path.Combine(Server.MapPath("~/Resume/"), ResumeName);
_resume.SaveAs(path);
model.ResumePath = "/Resume/" + _resume.FileName;
string fullPath = Request.MapPath("~" + oldResumePath);
if (System.IO.File.Exists(fullPath))
{
System.IO.File.Delete(fullPath);
}
}
model.FirstName = _candidate.FirstName;
model.LastName = _candidate.LastName;
model.Email = _candidate.Email;
model.ContactNo = _candidate.ContactNo;
model.SkypeId = _candidate.SkypeId;
model.CategoryId = _candidate.CategoryId;
model.YearId = _candidate.YearId;
model.DegreeId = _candidate.DegreeId;
model.CourseId = _candidate.CourseId;
model.StatusId = _candidate.StatusId;
model.SkillId = string.Join(",", _candidate.SkillIdArray);
model.ResumePath = _candidate.ResumePath;
_db.Entry(model).State = System.Data.Entity.EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("ListCandidate");
}
return View(_candidate);
}
View
@Html.LabelFor(model => model.ResumePath, "Resume :", new { @class = "control-label" })
<div>
@Html.TextBoxFor(model => model.ResumePath, new { @class = "form-control", type = "file", name = "_resume", tabindex = 12 })
</div>
<span>@Html.DisplayFor(model => model.ResumePath)</span>
@Html.ValidationMessageFor(model => model.ResumePath, "", new { @class = "text-danger" })