我开始使用 FubuMVC,我有一个简单的 Customer -> Order 关系,我正在尝试使用嵌套的部分显示。我的域对象如下:
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; }
}
我有以下控制器类:
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:(修剪标准的 html 内容)
<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>
当我查看客户/索引时,我看到以下内容:
Customers
Customer Name: John Smith
Orders: (1)
似乎在 CustomerPartial.ascx 中,执行 Model.Orders.Count() 正确地选择了存在 1 个订单。但是 PartialForEach(x => x.Orders) 没有,因为没有为订单呈现任何内容。如果我在 Order 构造函数上设置断点,我会看到它最初被 CustomersController 上的 Index 方法调用,但随后被 FubuMVC.Core.Models.StandardModelBinder.Bind 调用,因此它被 FubuMVC 重新实例化,并且丢失 Orders 集合的内容。
这不是我所期望的,我认为 PartialForEach 只会将域对象直接传递给部分。我在某处错过了重点吗?在 Fubu 中实现这种结果的“正确”方法是什么?
更新:如果有帮助,这是第一次点击 Order 构造函数时堆栈跟踪的前几行:
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]()