ASP.NET MVC: Avoid Polluting The Model Of The View With Error Messages

I recently answered a question that came up on the StackOverflow website. The question was whether using the ViewBag dynamic property of the controller to expose the error message was a good practice, as the OP investigations suggested that it was necessary to expose properties from the model. Obviously it is highly recommended NOT to use the ViewBag property since it does not provide any Strong Typing. If you want to communicate with the view you should always involve a typed model. The suggested solution is therefore legitimate in that respect, however, it is not a good practice when it comes to model error handling in ASP.NET MVC. The solution for exposing error messages (as in the previous example, which does not use data annotation attributes) derives from the use of the AddModelError method, from the ModelStateDictionary class. We do not need to instantiate this class because a ModelState property containing an instance of this class already exists in the Controller. Therefore, the right solution is the following:...

Lire la suite
10 Years of Programming

10 Years of Programming

I started programming when I was in high school. I learned a few lessons over the past decade, and I thought I would take a moment to gather my thoughts on these things. It took me about ten years and a lot of experimentation to figure out some of this....

Lire la suite
10 Ans De Programmation

10 Ans De Programmation

J'ai commencé la programmation quand j'étais au lycée. J'ai appris quelques leçons au cours de la dernière décennie, et je me suis dit que je prendrais un moment pour recueillir mes réflexions sur ces sujets. Il m'a fallu environ dix ans et beaucoup d'expérimentation pour assimiler certaines de ces choses....

Lire la suite
The Xamarin Revolution

The Xamarin Revolution

Xamarin is a cross-platform development tool. It solves dilemmas many developers face when developing cross-platform apps: separate coding languages and UI paradigms. With Xamarin, you can use C# for iOS, Android, and Universal Windows apps. And with Xamarin Forms, interface design for all three platforms can be accomplished within its XAML-based framework. Here is some documentation I would recommend if you consider getting started with Xamarin....

Lire la suite
Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Archives Mensuelles

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))