Good day guys,
I am trying to upload an image to a folder and save the path URL in the database using ASP.NET MVC code first approach.
At the point of saving the URL path, it is returning Object referenced Null.
Below is the action method called when the form is been saved.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Account_Number,Title,Firstname,Othername,Surname,Phone_Number,Bank_AccountName, Bank_Fullname, img_Passport")] SavingsAccount savingsAccount, HttpPostedFileBase postedFile)
{
Thread.Sleep(2000);
//Extract Image File Name.
string fileName = "1000000005";
//Set the Image File Path.
string filePath = "~/Images/Uploads/SavingsAccount/" + fileName;
//Save the Image File in Folder.
postedFile.SaveAs(Server.MapPath(filePath));
if (ModelState.IsValid)
{
savingsAccount.Account_Number = Account_No;
savingsAccount.Account_Type = "Savings Account";
savingsAccount.Account_Balance = 0;
savingsAccount.Date_Opened = Convert.ToDateTime(DateTime.Now.ToString());
savingsAccount.Opened_By = User.Identity.Name;
savingsAccount.img_Passport = filePath; //Insert the Passport URL Path to database
db.SavingsAccounts.Add(savingsAccount);
db.SaveChanges();
return View("DisplaySuccessMessage");
}
return View(savingsAccount);
}
The Posted file is been seen as Null when I used breakpoint to check value sent.
Am i doing it the wrong way?