MVC3 绑定自定义集合
根据: MVC3 模型将 pagedlist 绑定到 ViewModel自定义 EditorTemplate 和部分视图
请参阅上面的问题以获取代码片段
我现在遇到的问题围绕绑定自定义 IPgedList 集合。模型绑定器尝试将值绑定到 ViewModel 上的属性,但无法创建接口的实例(这并不奇怪)。
那么,如何在绑定值时通过实例化具体的 PagedList 类来将值绑定回我的 viewModel 呢?据我了解,IEnumerable 绑定器对列表或类似的派生程序执行此操作,那么我如何为我的自定义类/接口执行此操作?
我需要为此定制模型活页夹吗?如果是这样,任何有关此的信息或代码提示都很棒!
任何帮助都非常感谢。
更新:
更改 ViewModel 以包含一个覆盖的默认构造函数,该构造函数初始化接口,如下所示:
public class RequestCreateViewModel : ViewModelBase
{
public IPagedList<CreateRequestModel> PagedList { get; set; }
public RequestCreateVieWModel()
{
PagedList = new PagedList<RequestCreateModel>(new List<RequestCreateModel>(), new PagingOptions());
}
.. 似乎允许默认模型绑定器按照我的评论工作。但这似乎不是一个很好的解决方案,主要是因为我需要在每次创建 ViewModel 时推断 PagedList 对象的新对象参数。我的担心是不必要的吗?
As per : MVC3 Model binding pagedlist to ViewModel with custom EditorTemplate and Partial View
See above question for code snippets
The problem i am having now surrounds binding the custom IPagedList collection. 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).
So how can i bind values back to my viewModel by instantiating a concrete PagedList class when the values are bound? 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?
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:
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. Am i needlessly worrying?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 Codeplex 上 ASP.NET MVC 项目中
DefaultModelBinder.cs
的源代码。BindComplexModel
中的注释总结了一切:如果 MVC 框架对这些类型进行特殊处理,那么您将需要为您的类型创建一个自定义模型绑定器。但你提供的解决方案有效......为什么?您的类型未实现
ICollection
或IDictionary
特殊情况。默认代码路径调用模型类型的默认构造函数:您的默认构造函数创建您需要的类型。因此,没有错误。没有默认构造函数,没有对象实例,正如您所指出的,您会收到错误。
您询问更多想法。留下你所拥有的又如何?有用。此时编写自定义模型绑定器将需要更多工作。更多代码来完成同样的事情。
Look at the source for
DefaultModelBinder.cs
in the ASP.NET MVC project on Codeplex. The comment in theBindComplexModel
sums it all up:If the MVC framework special cases those types then you will need to create a custom model binder for your type. But the solution you provide works...why? Your type is does not implement the
ICollection
orIDictionary
special cases. The default code path calls the default constructor for a model type: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.