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.
source to share
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.
source to share