Question

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?

Was it helpful?

Solution

Summary is 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.Action call, all it's doing is instantiating the Cart class -- 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 Action call, it would populate that object. For example, if Cart contained a property called "Size" of type int, and your action call looked like:

@Html.Action("Summary", "Cart", new { Size = 5 })

Your Cart class would now have its Size property set to 5.

(Note also that it's far cleaner to use the Action method and not the RenderAction method in this case. As you've seen, RenderAction assumes a statement-level call, which requires those ugly enclosing curly braces. Action on the other hand is an expression and returns the actual HTML content inline, and thus can be expressed in a more concise fashion.)

OTHER TIPS

Summary is actually an action method, not a constructor. The data can come from a number of different places:

  • Route parameters
  • Query String
  • Form Variables
  • Etc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top