Why is FubuMVC new()ing up my view model in PartialForEach?
Posted
by Jon M
on Stack Overflow
See other posts from Stack Overflow
or by Jon M
Published on 2010-03-15T09:53:27Z
Indexed on
2010/03/15
15:29 UTC
Read the original article
Hit count: 216
fubumvc
I'm getting started with FubuMVC and I have a simple Customer -> Order relationship I'm trying to display using nested partials. My domain objects are as follows:
public class Customer
{
private readonly IList<Order> orders = new List<Order>();
public string Name { get; set; }
public IEnumerable<Order> Orders
{
get { return orders; }
}
public void AddOrder(Order order)
{
orders.Add(order);
}
}
public class Order
{
public string Reference { get; set; }
}
I have the following controller classes:
public class CustomersController
{
public IndexViewModel Index(IndexInputModel inputModel)
{
var customer1 = new Customer { Name = "John Smith" };
customer1.AddOrder(new Order { Reference = "ABC123" });
return new IndexViewModel { Customers = new[] { customer1 } };
}
}
public class IndexInputModel
{
}
public class IndexViewModel
{
public IEnumerable<Customer> Customers { get; set; }
}
public class IndexView : FubuPage<IndexViewModel>
{
}
public class CustomerPartial : FubuControl<Customer>
{
}
public class OrderPartial : FubuControl<Order>
{
}
IndexView.aspx: (standard html stuff trimmed)
<div>
<%= this.PartialForEach(x => x.Customers).Using<CustomerPartial>() %>
</div>
CustomerPartial.ascx:
<%@ Control Language="C#" Inherits="FubuDemo.Controllers.Customers.CustomerPartial" %>
<div>
Customer
Name: <%= this.DisplayFor(x => x.Name) %> <br />
Orders: (<%= Model.Orders.Count() %>) <br />
<%= this.PartialForEach(x => x.Orders) %>
</div>
OrderPartial.ascx:
<%@ Control Language="C#" Inherits="FubuDemo.Controllers.Customers.OrderPartial" %>
<div>
Order <br />
Ref: <%= this.DisplayFor(x => x.Reference) %>
</div>
When I view Customers/Index, I see the following:
Customers
Customer Name: John Smith
Orders: (1)
It seems that in CustomerPartial.ascx, doing Model.Orders.Count() correctly picks up that 1 order exists. However PartialForEach(x => x.Orders) does not, as nothing is rendered for the order. If I set a breakpoint on the Order constructor, I see that it initially gets called by the Index method on CustomersController, but then it gets called by FubuMVC.Core.Models.StandardModelBinder.Bind, so it is getting re-instantiated by FubuMVC and losing the content of the Orders collection.
This isn't quite what I'd expect, I would think that PartialForEach would just pass the domain object directly into the partial. Am I missing the point somewhere? What is the 'correct' way to achieve this kind of result in Fubu?
© Stack Overflow or respective owner