More bliss and joy in your .NET journeysBlog of an upside-down web developer, Art Skvira
Let’s dive right in, plain and simple: for some reason, you need to make sure that your users don’t enter any non-printable characters into a text input field on a form. Maybe the data needs to travel to far reaches of the known universe, to a remote ancient banking backend system that runs on COBOL and mainframes (fueled by the energy of a dying brown dwarf). Or maybe the column in the database where this data is going to end up has never heard of Unicode. Or maybe that’s just one of those unexplainable, irrational so-called “business rules”, that some aspiring manager somewhere up in the ranks decided to implement.
It’s a fine day, your site runs smoothly, thanks to Jekyll with its static HTML generation and your clean and clever markdown. Then for some reason you decide to have a look at the Google Analytics Organic Search report and suddenly you see that your traffic just jumped off the cliff.
Sometimes, when errors or exceptions happen you can’t just attach a debugger to your web app - for example, errors may happen only in production, and you can only sit and watch the error logs fly past, or sometimes there are even no meaningful logs available. Maybe you need to debug a closed system - proprietary component/NuGet package that fails. Maybe it’s a combination of several factors
Quite often people stumble into same problems again and again, and getting a NullReferenceException is the one that occurs most frequently, and frankly, can be quite annoying. This problem happens when writing brand-new ASP.NET MVC code, such as controllers or views, but also when modifying existing code that used to work just fine, but somehow suddenly got broken. Here I want to show you why these exceptions happen and how to fix them, so you can stop wasting your time and do more of the programming that you actually enjoy.
Recently, when writing code for my blog post on drop downs, “DropDownListFor with Dictionaries in
ASP.NET MVC and why SelectList wants to kill you”, I stumbled over an interesting problem –
when using ASP.NET MVC HTML helpers, such as
render controls and
@Html.ValidationMessageFor() to render validation error messages, I realised
that ASP.NET MVC uses its own CSS classes, so no errors are getting highlighted when using Bootstrap
I would like to show you how to use ASP.NET MVC helper function
class with generic Dictionaries, such as
Dictionary<string, string> or
Dictionaries can be quite useful for a number of scenarios – serving as a data source for select
lists of countries, states, time zones, age ranges, genders – basically any pre-defined, fixed-set
ASP.NET MVC is a very powerful, yet quite complex (if not complicated) web development framework. There’s a heaps of various namespaces, classes and functions. It’s bloody hard to figure out which particular class or function overload you need to use for your specific task.
There probably was only a handful of subjects at school and later at uni that I liked - and I despised or ignored the rest, doing everything I could just to get a good enough grade to get to the next term. It wasn’t all that different at work after that - whenever something boring had to be done, I’d distract myself with whatever I could - news reading, social media, pretending to ‘learn’ by reading tech articles/books. Sometimes that worked out ok, other times it totally sucked due to the stress of looming deadlines.
It’s surprising how many subtle, but frustrating traps one can fall into when building sites with ASP.NET MVC. Creating forms for the web is one of them. It’s common to spend hours on something trivial, such as displaying a selected value in a DropDownList on postback, or getting that selected value in a controller. Quite often it happens when you just start learning ASP.NET MVC or upgrade from an older tech. And boy, is this frustrating as hell – instead of building an actual web app, you spend hours wrestling with the framework.
When my old leather wallet started falling apart I decided I could do without another piece of an animal’s skin. Turns out in this day and age you don’t need to kill a cow to make a wallet - just use something synthetic, like tyvek.