Pass Multiple Models in a View in ASP.NET MVC

I came across this very intuitive article in CodeProject. This article explains how to pass multiple models in a view using ASP.NET MVC4, describing the pros and cons of these approaches and with a sample demo project as well.

  1. ViewModel
  2. Partial View
  3. ViewBag
  4. ViewData
  5. TempData
  6. Tuple

Link to Code Project Articles:

I found it very helpful as I am on the verge of building a ASP.NET MVC project and am diving into its nitty-gritty details.

Hope it helps you too! 🙂

Advertisements

Mocking HttpContext for unit tests

In this post, I will share the problem that I stumbled upon while writing unit tests for the code block that pulls values off the HttpContext. And yes, I started out writing unit tests finally, after several years of .NET programming.

Problem:

I needed to unit test a module that pulls a value (in my case, it was the IP address of a client machine) off the HttpContext.

Image

The problem was that when I tried to unit test this code block, I could not mock HttpContext directly since it is sealed. Also, I kept getting null reference exception as the current context did not exist when called from the unit test method. So, my unit test threw exception and did not run and kept on turning RED. 😦

Continue reading