首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么FubuMVC new()在PartialForEach中创建我的视图模型?

为什么FubuMVC new()在PartialForEach中创建我的视图模型?
EN

Stack Overflow用户
提问于 2010-03-15 17:53:28
回答 1查看 372关注 0票数 2

我刚开始使用FubuMVC,我有一个简单的Customer ->订单关系,我正尝试使用嵌套的分式来显示。我的域对象如下:

代码语言:javascript
复制
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; }
}

我有以下控制器类:

代码语言:javascript
复制
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内容)

代码语言:javascript
复制
<div>
    <%= this.PartialForEach(x => x.Customers).Using<CustomerPartial>() %>
</div>

CustomerPartial.ascx:

代码语言:javascript
复制
<%@ 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:

代码语言:javascript
复制
<%@ Control Language="C#" Inherits="FubuDemo.Controllers.Customers.OrderPartial" %>
<div>
    Order <br />
    Ref: <%= this.DisplayFor(x => x.Reference) %>
</div>

当我查看Customers/Index时,我看到以下内容:

代码语言:javascript
复制
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构造函数时堆栈跟踪的前几行:

代码语言:javascript
复制
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()

第二次:

代码语言:javascript
复制
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]()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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>() %>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2446210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档