Tuples (Magic In Views)

Many times we come across a situation in MVC when we require two different Models in a strongly typed View. This need usually arise when we have a requirement to use properties of two different models in a single view.

Problem : Can we really have two or more models in a strongly typed View???


And the solutions that you will find in most of the places is that we can create a View Model (Basically a combination of two models with required properties from both the Models). View Models have lot of benefits as they save the network data by including only the required properties.

But in case if someone want to use two or more Models in a single View so is it possible?
Yes it is possible through Tuples.true 
By using Tuples we can include two or more than two models in a single view.

Theoretically the view will be strongly typed to a single entity but practically that entity will be a combination of two or more models.

Let’s walk through with the code now.

Code Analysis :

View :

Declaration of tuple

@model Tuple<ModelFirst, ModelSecond>

Here ModelFirst and ModelSecond are two different models with whom our view is strongly typed.

Usage in the Razor View Engine:

@Html.TextBoxFor(m => m.Item1.StoreId, new { id = "last" })

@Html.TextBoxFor(m => m.Item2.PromotionLocation)

The models will be accessed as Item1 and Item2 . If there are more then two models the usage will go on as Item3 , Item4 …

So here in the two Textboxes, we are using two different properties (StoreId and PromotionLocation) which are present in two different models.

Global.asax entry:

We have to create a constructor class for tuple model as it doesn’t have any default constructor creation and we have to register the constructor as a default binder in Global.asax.

ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder();

Defining constructor class:

This class must be inheriting the DefaultBinder

public class MyDefaultModelBinder:DefaultModelBinder
protected override object CreateModel(ControllerContext controllerContext,
ModelBindingContext bindingContext, Type modelType)
if (modelType == typeof(Tuple<Models.ModelFirst, Models.ModelSecond>))
return new Tuple<ModelFirst, ModelSecond>(new ModelFirst(), new ModelSecond());
return CreateModel(controllerContext, bindingContext, modelType);

Controller actions:

Initializing the View:

public ActionResult TupleMode()
var tuple = new Tuple<ModelFirst, ModelSecond> (new ModelFirst()
{StoreId=1,promotionStartDate=5 }, new ModelSecond()
{ promotionId = 1, promotionName = "First", PromotionLocation = "Bang" });
return View(tuple);

For performing operations on Form Submit:

public JsonResult CreateContact(Tuple<ModelFirst, ModelSecond> tuple)
return Json(tuple.Item1.StoreId +"--"+tuple.Item2.PromotionLocation);

As a readability point of view, off-course this approach sucks as the models will be accessed by Item+Corresponding Number in the View or controller but then it provides you the unique way of using multiple models in a view without creating ViewModels.

This is how we can practically have two or more models in a strongly typed view of MVC. So the next time if someone ask you that in MVC can we have Two or more models in a strongly typed view, I guess you have the answer now.. 😉

Written By: Nitin Rawat, Software Developer, Mindfire Solutions

Posted on February 3, 2014, in MVC and tagged , , , , , . Bookmark the permalink. 3 Comments.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: