An easy way to merge to RouteValueDictionary objects in ASP.NET MVC4

I wrestled with this problem for a while and finally found a solution I’m happy enough to post here.

Q. Given two classes, is it possible to bundle up their properties into a RouteValueDictionary and pass the results to the RouteAPI or via an ActionLink?

YES!

This technique merges to RouteValueDictionary objects using the Union method.

Given the following c# classes…

public class Student
{
public int StudentID { get; set; }
public string FirstName { get; set; }
public string LastNamse { get; set; }
public string Email { get; set; }
public DateTime DateOfBirth { get; set; }
}

public class Address
{
public string StreetLine1 { get; set; }
public string StreetLine2 { get; set; }
public string Suburb { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
}

…you can easily write out the key/value pairs to a RouteValueDiction object like this:

RouteValueDictionary routeStudent = new RouteValueDictionary(aStudent);
RouteValueDictionary routeAddress = new RouteValueDictionary(homeAddr);

Here’s the tricky part that I’ve not seen anywhere:

RouteValueDictionary routeCombined = new RouteValueDictionary(routeStudent.Union(routeAddress).ToDictionary(k => k.Key, k => k.Value));

Output to the debug window to prove it all works…

foreach (KeyValuePair<string, object> n in routeCombined)
	System.Diagnostics.Debug.WriteLine(n.Key + ": " + n.Value);

…or simply bundle the RouteValues into an ActionLink:

@Html.ActionLink("Click here to pass routeValues to RouteAPI", "MyActionName", routeCombined);

…or otherwise pass to the RouteAPI in your code somewhere.

return this.RedirectToRoute(routeCombined);

If you have this helpful, please tell the world. Also see this same answer on StackOverFlow

The New Firewall

Welcome to the new Firewall that is blocking you from where you want to be.¬† How would you login if my website offered you all of these choices and you already had several accounts? It’s just a bit crazy I tells you. I like using Google so Google and consolidate everything for me, but what if I really want to interact with both Google and Instagram on a website, which begs the question, why on Earth would¬† a website offer multiple choices to login in the first place? It probably won’t benefit me at all? Hmm… not sure about that. Tell me your thoughts.

The New Firewall Craziness!
The New Firewall Craziness!

Links for MVC development

Today’s links on MVC development and related technologies, read at your own peril ; )

  • General Discussion

    http://www.dnnsoftware.com/community-blog/cid/155035/the-future-of-dnn-speaks-razor–1-you-have-to-migrate-now

  • Javascript

    https://angularjs.org/

  • CSS

    http://getbootstrap.com/

    http://foundation.zurb.com/

    http://metroui.org.ua/

Why you need to sign up to Microsoft’s BizSpark program now!

I recently purchased Windows 8.1 but as it turns out, perhaps I didn’t need too.

I recently signed up for Microsoft’s BizSpark program to develop software solutions for my customers. I was accepted, and within a few day I was gifted with over 900 products from Microsoft’s software library and hundreds of licence keys… essentially for free! Included in the MSDN Library of downloads was Windows 8.1 – Doh!

Oh well… I’ve got my sights on the bigger picture. I was also given $160 worth of credit per month, for three years, to spend on Windows Azure cloud. This is pretty cool. It’s early stages so far, but it appears that this blog will cost me about $20/month to run on Windows Azure… which is about on par with Amazon’s offerings.

I’ve decided to blog my experience with Windows Azure, it’s various cloud offerings and my experience creating software for my customers and market.

If you are a software developer struggling to afford client or server software from Microsoft, then I strongly urge you to seek out the BizSpark program and sign up now!

Test data generator

Background

I’m currently designing an MVC ASP.NET web application with a SQL Server database.

I want my application to process hundreds or thousands of transactions every hour. That being the case, I need to stress test every part including the web server, slq database, indexes, paging, filtering and many other parts of my application.

test data

Solution

I developed a c++ console application that will generate as many test records as I want (into the millions). The file below contains about 100,000 test records containing randomized first and last names. (The last names are country specific and the first names are gender specific).

  1. Download Test Data.
    TestData.zip
  2. Unzip file and import into a temporary table in SQL Server.
    (Note to the Novice: Creating a temporary table might seem like a wasted step, but it’s actually the correct way of doing this).
    USE MyDatabaseName
    GO
    
    CREATE TABLE CSVImport (FirstName nvarchar(20),
    LastName nvarchar(20),
    Gender nvarchar(7),
    Country nvarchar(20))
    GO
    
    BULK INSERT MyDatabaseName.dbo.CSVImport
    FROM "C:TestData.txt"
    WITH (FIELDTERMINATOR=',', ROWTERMINATOR= 'n')
    GO
    
  3. Inserted the imported data into an existing table in your database.In this example, the ‘People2′ table.
    INSERT INTO People2 (FirstName, LastName)
    SELECT FirstName, LastName FROM CSVImport
    GO
    

Amazon Cloud is my new best friend

After a long hiatus, my website is back online and hosted in the Amazon cloud at Sydney (aws.amazon.com). I’m gradually migrating all my computer services to the Ubuntu Operating System (LTS) both at home and professionally. My new home office is located in Boorowa and with a bit of luck I’ll get back into programming soon.

BoorowaSunset
Sunset at Boorowa, NSW

Working on the Gold Coast – Part 1

When I worked on the Gold Coast, I was responsible for the networking infrastructure, maintenance and technical support for 250 workstations and 33 critical business servers spanning 18 companies across multiple sites around the Gold Coast and Australia. Daily duties included troubleshooting applications, systems, networks and helping non-technical users over the phone and in person. To learn more about my employment visit my LinkedIn profile.

WorkingOnTheGoldCoastTypical Software Managed while I worked on the Gold Coast included:

  • Microsoft Business Server
  • Microsoft Server 2003 and 2008
  • Microsoft XP, Vista, Windows 7 desktops
  • Microsoft Exchange Server… plus many others.

It was crazy fun, hectic, stressful and rewarding all at the same time. My time there was short, but I enjoyed myself. Despite all the technology, the best part was working within walking distance to Broadbeach where I would frequently have lunch. It was also nice working close to a large shopping center as my previous employment at Mt. Stromlo had no shops (and frequently no people either)… but that’s another story.

error C2065: 'M_PI' : undeclared identifier

Problem

error C2065: 'M_PI' : undeclared identifier

If you’ve checked the obvious stuff and you still can’t get M_PI declared, it’s probably because another file or library has already included the header before you, but didn’t include the necessary macro.

Solution

Instead of the following:

#define _USE_MATH_DEFINES
#include

…include the macro in the Projects Preprocessor Definitions. In this way, the macro will be included at compile time solving the problem

Professional Software Lovingly Handcrafted by Zen-Masters of the Software Trade