我开始使用FubuMVC并且我有一个简单的客户 - >订单关系我试图使用嵌套的部分显示。我的域对象如下:
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 =&gt; x.Orders)没有,因为没有为订单呈现任何内容。如果我在Order构造函数上设置了一个断点,我看到它最初是由CustomersController上的Index方法调用的,但后来它被FubuMVC.Core.Models.StandardModelBinder.Bind调用,所以它被FubuMVC重新实例化了丢失Orders集合的内容。
这不是我所期望的,我认为PartialForEach只会将域对象直接传递给partial。我错过了某个地方的观点吗?在Fubu中实现这种结果的“正确”方法是什么?
更新:如果它有帮助,这是第一次命中构造函数时,堆栈跟踪的前几行:
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()
第二次:
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]()
答案 0 :(得分:2)
强:
PartialForEach不会新建新模型,它会传递您传递的模型(即x.Orders)。
您遇到的问题似乎是因为您没有在CustomerPartial.ascx中添加“.Using”
在CustomerPartial.ascx中,更改
<%= this.PartialForEach(x => x.Orders) %>
到
<%= this.PartialForEach(x => x.Orders).Using<OrderPartial>() %>