问题描述
在最近的一个项目 - 我已经打了一个意想不到的障碍结果。
用简单的公共领域的类(注意不是属性)似乎并不想打的不错
ASP.net MVC 3.0模型粘合剂。
这设计是?
除了修改字段属性 - 这里的任何选项
更新
原因很简单域(而不是属性)是因为我与MVC和脚本夏普项目之间共享库工作。脚本尖锐支持属性 - 但它成为与JavaScript一个烂摊子在视图(使用knockout.js)
因此,尽管我很想(这是只使用属性)我使用的公共领域在我的DTO类。
我想,以避免同一类的多个定义。叹息
Yes, only properties with public getter/setters work with the default model binder.
That's what you should do as you should be using view models anyway. And view models are specifically designed for the views. So changing the fields to properties is the correct way to do. Now if you don't follow good practices and try to use your domain models as input/output to actions then you will have to write a custom model binder that works with fields. It will be a lot of work though.
这篇关于ASP.net MVC - 模型绑定排除类字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!