My requirement is to read data from excel file (client side) and post the JSON to server which will create enteries in one of the DB table. Before creating records I need to validate data at server side. If I add validation attribute to DTO, the framework automatically validate the object and through errors while I need them to be formatted or represented in better way. How can I handle that.
Also, I was trying the rely on Entityframework validation which will give errors if something wrong is found so I am catching DBExceptions like below and want to return errors the way I want to:
catch (Exception e) { var exc = e as DbEntityValidationException; foreach (var eve in exc.EntityValidationErrors) { var message = string.Format("For member {0} validation {1}{2} found", member.UserName, exc.EntityValidationErrors.Count(), exc.EntityValidationErrors.Count()>1? "errors":"error"); foreach (var ve in eve.ValidationErrors) { message = message + ve.ErrorMessage; errors.Add(message); } } } } return errors;
but on the UI I dont get the error results though still get the system generated error message.
Can someone please help me with this?
1 Answer(s)
-
0
Hi,
- <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Validating-Data-Transfer-Objects#disabling-validation">http://www.aspnetboilerplate.com/Pages/ ... validation</a> (use [DisableValidation] attribute for a method to disable validation).
- You can disable exception handling (<a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/MVC-Controllers#exception-handling--result-wrapping">http://www.aspnetboilerplate.com/Pages/ ... t-wrapping</a>) or manually try-catch exception and throw a userfriendly exception. The key point here is that: You should call CurrentUnitOfWork.SaveChanges() in order to immediately save changes (and thus make the EF validation).