Is it possible to provide ASP.NET MVC User Control with a different model than page

I have an order page that is rendered from a model object (Order) with multiple 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 a different User Control for each OrderComment (named OrderCommentControl). If I could use a relay for each collection, that would be great, but I ran into a problem. I want my custom control declarations to look like this:

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

      

I am getting an error when I try to do the following:

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

I'm guessing that a repeater might not be the right way to go, but I really want every custom control to have a model of type OrderItem or OrderComment, not just Order.

+1


source to share


2 answers


Yes. By 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 a real source at http://www.codeplex.com/aspnet . Click the Source tab and navigate to the MVC source tree.

+3


source


Make sure your call to render the custom control passes the required data to the Render function.

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



See this old post from Rob Conyeri

0


source







All Articles