I’m using MVC3, with the Razor syntax, and am going over an example in the Apress book “Pro ASP.Net MVC 3 Framework” (which is very good). In it the author has this bit in a _Layout.cshtml file:
<body>
<div id="header">
@{Html.RenderAction("Summary", "Cart");}
<div class="title">SPORTS STORE</div>
</div>
<div id="categories">
@{ Html.RenderAction("Menu", "Nav"); }
</div>
<div id="content">
@RenderBody()
</div>
</body>
My question has to do with the CartController class. I see that it’s passed a Cart object in its constructor, but the code in the _Layout.cshtml snippet above doesn’t appear to pass one in when RenderAction() is called?
CartController snippet:
public ViewResult Summary(Cart cart) {
return View(cart);
}
When I debug that method a Cart object is in fact passed in, but I don’t see where it’s coming from. This sample code uses Ninject as a dependency injection container but I can’t find any evidence in the configuration that it knows anything about the Cart class. Is there a ‘default value’ mechanism in MVC?
Summaryis an action method. It is not a constructor. Therefore, DI doesn’t have anything to do with this. Instead, what we are seeing is the behavior of the default model binder. That is to say, the model binder will analyze whatever parameters you declare for your action method and will attempt to populate these arguments with data derived from form or query string parameters and route values.In your particular scenario, since you are not passing in any route values to your
Html.Actioncall, all it’s doing is instantiating theCartclass — if you inspect it you will presumably see that it’s empty (no properties have been set).However, if you were to pass route values into your
Actioncall, it would populate that object. For example, ifCartcontained a property called “Size” of type int, and your action call looked like:Your
Cartclass would now have itsSizeproperty set to 5.(Note also that it’s far cleaner to use the
Actionmethod and not theRenderActionmethod in this case. As you’ve seen,RenderActionassumes a statement-level call, which requires those ugly enclosing curly braces.Actionon the other hand is an expression and returns the actual HTML content inline, and thus can be expressed in a more concise fashion.)