Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Errors (Exception Messages) from Repository to Controller Action in asp.net MVC2
    text
    copied!<p>I have been trying to develop an app using the repository pattern like in the Nerd Dinner application however I would like to handle exceptions in the repository and pass the exception messages back to the controller so that I can output the message in a nice page to the user. </p> <p>How do I pass back this exception message, or even pass that an exception has occured in the repository.</p> <p><a href="http://www.asp.net/ajaxlibrary/jquery_errors.ashx" rel="nofollow">http://www.asp.net/ajaxlibrary/jquery_errors.ashx</a></p> <p>In the following example from the above url, "_repository.HasErrors" is used as a check but I want to know what the implementaion of this is in the repository in C# as I dont know how this is implemented and also if its possible to also get the error message as well.</p> <pre><code>01.// GET: /HandlejQueryErrors/Contact/Create 02.public ActionResult Create() 03.{ 04. return View(); 05.} 06. 07.// POST: /HandlejQueryErrors/Contact/Create 08.[HttpPost] 09.public ActionResult Create(ContactViewModel viewModel) 10.{ 11. var response = new AjaxResponseViewModel(); 12. 13. try 14. { 15. var contact = _repository.SaveOrUpdate(viewModel); 16. if (!_repository.HasErrors) 17. { 18. response.Success = true; 19. response.Message = "Your contact was successfully created!"; 20. } 21. else 22. { 23. response.Message = "There was an error updating your contact!"; 24. } 25. } 26. catch (Exception exception) 27. { 28. response.Success = false; 29. response.Messages exception.Message; 30. } 31. 32. return Json(response); 33.} </code></pre> <p>Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload