I am using MVC. I have 'Contact' Table in which I have PhotoID filed. I want save data and if Phoid filed is empty then save default path like '~/Uploads/UploadImages/Guest.PNG' how can I do? my code is below
- [HttpPost]
- [ValidateAntiForgeryToken]
- public ActionResult CreateContacts(Contact contacts, HttpPostedFileBase ImageFile)
- {
-
- if (ModelState.IsValid)
- {
- string fileName = Path.GetFileNameWithoutExtension(ImageFile.FileName);
- string extension = Path.GetExtension(ImageFile.FileName);
- fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
- contacts.PhotoID = "~/Uploads/UploadImages/" + fileName;
- fileName = Path.Combine(Server.MapPath("~/Uploads/UploadImages/"), fileName);
- ImageFile.SaveAs(fileName);
-
- using (ESContext db = new ESContext())
- {
- db.Contacts.Add(contacts);
- db.SaveChanges();
- return RedirectToAction("IndexContacts");
- }
-
- }
-
-
- return View(contacts);
- }