我刚开始使用FubuMVC,我有一个简单的Customer ->订单关系,我正尝试使用嵌套的分式来显示。我的域对象如下:
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/Index时,我看到以下内容:
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()第二次:
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]()发布于 2010-03-15 23:28:14
乔恩:
PartialForEach不会更新新模型,它会传递您传递的模型(即x.Orders)。
看起来您面临的问题是因为您没有在CustomerPartial.ascx中添加".Using“
在CustomerPartial.ascx中,更改
<%= this.PartialForEach(x => x.Orders) %>
至
<%= this.PartialForEach(x => x.Orders).Using<OrderPartial>() %>
https://stackoverflow.com/questions/2446210
复制相似问题