views:

320

answers:

2

I have an order page that is being rendered from a Model object (Order) with a few properties. One of the properties of the Order object is

public List<OrderItem> Items { get; set; };

and the other is

public List<OrderComment> Comments { get; set; };

My main page is declared like this:

public class OrderView : ViewPage<Order>

I want to have a User Control for each OrderItem (named OrderItemControl), and another User Control for each OrderComment (named OrderCommentControl). If I could use a repeater for each collection then that would be great, but I am running into a problem. I want my user control declarations to looks like this:

public class OrderItemControl : ViewUserControl<OrderItem>
public class OrderCommentControl : ViewUserControl<OrderComment>

I get an error when I try to do this saying:

{"The model item passed into the dictionary is of type 'Order' but this dictionary requires a model item of type 'OrderItem'."}

I am guessing repeater might not be the right way to go, but I really want each User Control to have a model of type OrderItem or OrderComment, and not just Order.

A: 

Make sure your call to render the user control passes the right data the Render function.

<%=Html.RenderUserControl(“~/UserControls/OrderItem.ascx”,ViewData.Model.OrderItems[index])%>

(My memories of the current object model for ViewData are fuzzy, but that should be close).

See this old post from Rob Conery

JasonTrue
+2  A: 

Yes. Using RenderPartial you can use the signature that supplies the model and simply use whatever model object is available to you in the current View.

<% foreach (OrderItem orderItem in ViewData.Model.OrderItems)
   {
 %>
       <%= Html.RenderPartial( "OrderItem", orderItem, ViewData ) %>
<% } %>

You can poke around in the actual source at http://www.codeplex.com/aspnet. Click on the Source tab and navigate down to the MVC source tree.

tvanfosson
You shouldn't need to specify the path to your user control unless they're in a non standard location.
Todd Smith
@Todd Smith -- I wasn't sure and didn't have a sample in front of me. Thanks. Now, fixed.
tvanfosson