开发者

Why is FubuMVC new()ing up my view model in PartialForEach?

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).Using<OrderPartial>() %>
</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 Ord开发者_如何学Cers 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?

Update: In case it helps, this is the top few lines of the stacktrace the first time the Order constructor gets hit:

at FubuDemo.Customer..ctor()
at FubuDemo.Controllers.Customers.CustomersController.Index(IndexInputModel inputModel)
at lambda_method(ExecutionScope , CustomersController , IndexInputModel )
at FubuMVC.Core.Behaviors.OneInOneOutActionInvoker`3.performInvoke()

And the second time:

at FubuDemo.Customer..ctor()
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.Activator.CreateInstance(Type type)
at FubuMVC.Core.Models.StandardModelBinder.Bind(Type type, IBindingContext context)
at FubuMVC.Core.Runtime.ObjectResolver.BindModel(Type type, IBindingContext context)
at FubuMVC.Core.Runtime.ObjectResolver.BindModel(Type type, IRequestData data)
at FubuMVC.Core.Diagnostics.RecordingObjectResolver.BindModel(Type type, IRequestData data)
at FubuMVC.Core.Runtime.FubuRequest.<>c__DisplayClass2.<.ctor>b__0(Type type)
at FubuMVC.Core.Util.Cache`2.Retrieve(KEY key)
at FubuMVC.Core.Util.Cache`2.get_Item(KEY key)
at FubuMVC.Core.Runtime.FubuRequest.Get[T]()


Jon:

PartialForEach does not new up new models, it passes the models you pass (i.e. x.Orders).

It looks like the problem you're facing is because you did not add ".Using" in your CustomerPartial.ascx

In CustomerPartial.ascx, change

<%= this.PartialForEach(x => x.Orders) %>

to

<%= this.PartialForEach(x => x.Orders).Using<OrderPartial>() %>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜