简体   繁体   English

MVC3绑定自定义集合

[英]MVC3 Binding a custom Collection

As per : MVC3 Model binding pagedlist to ViewModel with custom EditorTemplate and Partial View 按照: MVC3模型将页面列表绑定到具有自定义EditorTemplate和Partial View的ViewModel

See above question for code snippets 有关代码段,请参见上面的问题

The problem i am having now surrounds binding the custom IPagedList collection. 我现在遇到的问题是绑定自定义IPagedList集合。 The model binder attempts to bind the values to the property on the ViewModel but is unable to create an instance of the interface (no suprises there). 模型绑定程序尝试将值绑定到ViewModel上的属性,但是无法创建接口的实例(此处未提供)。

So how can i bind values back to my viewModel by instantiating a concrete PagedList class when the values are bound? 那么,如何在绑定值时实例化具体的PagedList类,将值绑定回我的viewModel? As i understand it the IEnumerable binder does this for a List or similar derivitive, so how can i do this for my custom class/interface? 据我了解,IEnumerable活页夹是针对列表或类似的派生类这样做的,那么我该如何为我的自定义类/接口做呢?

Do i need a custom model binder for this? 为此,我需要自定义模型活页夹吗? If so any information or code tips on this is great! 如果是这样,那么任何有关此方面的信息或代码提示都很棒!

Any help greatly appreciated thanks. 任何帮助,非常感谢。

Update: 更新:

Changing the ViewModel to include an overriden default constructor which initialises the Interface like so: 更改ViewModel使其包含重写的默认构造函数,该构造函数将初始化接口,如下所示:

public class RequestCreateViewModel : ViewModelBase
{
    public IPagedList<CreateRequestModel> PagedList { get; set; }

    public RequestCreateVieWModel()
    {
        PagedList = new PagedList<RequestCreateModel>(new List<RequestCreateModel>(), new PagingOptions());
    }

.. appears to allow the default model binder to work as per my comment. ..似乎允许根据我的评论使用默认模型联编程序。 But it doesnt seem like a great solution, mainly because im needing to infer new object parameters for the PagedList object each time a ViewModel is created. 但这似乎不是一个很好的解决方案,主要是因为每次创建ViewModel时,我都需要为PagedList对象推断新的对象参数。 Am i needlessly worrying? 我不用担心吗?

Look at the source for DefaultModelBinder.cs in the ASP.NET MVC project on Codeplex . Codeplex上的ASP.NET MVC项目中查看DefaultModelBinder.cs的源代码。 The comment in the BindComplexModel sums it all up: BindComplexModel的注释将BindComplexModel总结为:

// special-case IDictionary<,> and ICollection<>

If the MVC framework special cases those types then you will need to create a custom model binder for your type. 如果MVC框架对这些类型有特殊情况,那么您将需要为您的类型创建自定义模型绑定程序。 But the solution you provide works...why? 但是您提供的解决方案可行...为什么? Your type is does not implement the ICollection or IDictionary special cases. 您的类型不实现ICollectionIDictionary特殊情况。 The default code path calls the default constructor for a model type: 默认代码路径调用模型类型的默认构造函数:

// fallback to the type's default constructor
return Activator.CreateInstance(typeToCreate);

Your default constructor creates the type you need. 您的默认构造函数创建所需的类型。 Therefore, no error. 因此,没有错误。 No default constructor, no object instance and as you have pointed out, you get an error. 没有默认的构造函数,没有对象实例,并且正如您所指出的,您将得到一个错误。

You asked for more ideas. 您要求更多的想法。 How about leaving what you have. 剩下的东西怎么样。 It works. 有用。 Writing a custom model binder would just be more work at this point. 此时,编写自定义模型活页夹将需要更多工作。 More code to accomplish the same thing. 更多代码可以完成同一件事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM