From zero to fully-functioning web application in 3 weeks, user authentication, role management, file upload, and pretty front end included.
After 3 weeks of intensive work, my ticket system project is done (famous last words)! It needed a name, so I dubbed it IRIS (Inquiry and Report Information System). IRIS pulls together many things I’ve been introduced to since starting this class, including:
Here are just a few of the things I learned while working on IRIS:
ViewModels - .NET is very helpful in scaffolding out CRUD controllers and views for you, but the downside is that maybe you don’t want to expose all of your database record’s fields in a view (even as hidden fields), or maybe you need a view that doesn’t really square up with a particular database object. In these scenarios you can create a ViewModel containing only the fields you need in your view, and use that to specify the fields you want to send to and from your view via the controller.
Using ViewModels also protects your application from overposting, which is something nefarious individuals can do to attempt to manipulate variables they are not supposed to have access to.
PagedList.MVC - adding paging functionality to an MVC app is trivial if you use the NuGet PagedList.MVC package and follow the instructions in this useful ASP.NET article, which also explains how to set up filtering of results based on a user clicking on a particular field. This isn’t the only way to do accomplish this, but it’s a pretty handy one.
On the more obscure but very-useful-when-you-need-it end:
AsNoTracking - If you have a user edited version of a record in hand, and you want to compare it to the original, un-edited version, it’s important to retrieve the un-edited version using the AsNoTracking method. Otherwise, since both copies of the record have the same Id (primary key), .NET gets confused and weird things start happening - what you do to one of them can affect the other, even though you’ve loaded them into separate variables. It’s .NET automation trying to be overly helpful with undesirable results, and something I learned the hard way. It looks something like this:
var oldTicket = db.Tickets.AsNoTracking().Single(m => m.Id == id);
TargetMigration:0 - If you’re using Entity Framework code first migration and your working database has gotten out of control (perhaps you’ve adjusted the structure a bit more than EF can understand and it won’t run an automatic migration), you can wipe out your DB and reset it to pristine (and empty) current structure with one command from the Visual Studio Package Manager console:
Update-Database -TargetMigration:0 -f
This won’t run your seed method, so if you have one, run Update-Database again with no arguments.
That concludes this week’s list of interesting and obscure MVC .NET tidbits. Be sure to check out IRIS and be amazed at what you can turn out in a mere 3 weeks of burning the candle at both ends if you employ Visual Studio, MVC .NET, and Entity Framework, plus of course Bootstrap to make it all look pretty.