public ActionResult FormData(TestDataModel model) { try { if (ModelState.IsValid) { if (model.Id > 0) { var entity = _dbContext.TestDatas.Find(model.Id); entity.Id = model.Id; entity.FirstName = model.FirstName; entity.LastName = model.LastName; entity.Email = model.Email; entity.DOB = model.DOB; entity.Gender = model.Gender; entity.mobile = model.mobile ?? 0; _dbContext.Entry(entity).State = EntityState.Modified; _dbContext.SaveChanges(); } else { var entity = new TestData(); entity.FirstName = model.FirstName; entity.LastName = model.LastName; entity.Email = model.Email; entity.DOB = model.DOB; entity.City = model.City; entity.Gender = model.Gender; entity.mobile = model.mobile ?? 0; _dbContext.TestDatas.Add(entity); _dbContext.SaveChanges(); } return(Json(new { result = true }, JsonRequestBehavior.AllowGet)); } else { return(Json(new { result = false, message = "Check Data." }, JsonRequestBehavior.AllowGet)); } } catch (Exception exception) { return(Json(new { result = false, message = "Something went wrong. Pleas try Again." }, JsonRequestBehavior.AllowGet)); } }
protected override void OnException(ExceptionContext filterContext) { ViewResult view = new ViewResult(); view.ViewName = "Error"; filterContext.Result = view; filterContext.ExceptionHandled = true; //---Log--- Exception ex = filterContext.Exception; var sb = new StringBuilder(); var context = System.Web.HttpContext.Current; var exceptionType = ex.GetType().ToString(); var exceptionTypeSplit = exceptionType.Split('.'); exceptionType = exceptionTypeSplit[exceptionTypeSplit.Length - 1]; sb.Append(exceptionType).Append(": ").Append(ex.Message); sb.Append(ex.StackTrace); var innerException = ex.InnerException; if (innerException != null) { var innerExType = innerException.GetType().ToString(); var innerExTypeTypeSplit = innerExType.Split('.'); innerExType = innerExTypeTypeSplit[innerExTypeTypeSplit.Length - 1]; sb.Append("\r\nINNER EXCEPTION: ").Append(innerExType).Append(": ") .Append(innerException.Message).Append(innerException.StackTrace); } var logEntity = new ErrorLog() { IP_Address = Request.UserHostAddress, Message = sb.ToString(), Date = DateTime.UtcNow, }; try { _dbContext.ErrorLogs.Add(logEntity); _dbContext.SaveChanges(); //logEntity.ErrorId can also be made use of in any way. } catch { // Email logic } }