Tuples In MVC

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 the properties of two different models in our view.



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



And the solutions that you will found 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:

By Using Tuples we can include two or more then 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.

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:

[HttpGet]
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:

[HttpPost]
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 MVCSo 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 😉


In my next blog I will be coming up with a new magic in MVC.

Advertisements

2 thoughts on “Tuples In MVC

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s