ASP.net MVC - 模型绑定排除类字段? [英] ASP.net MVC - Model binding excludes class fields?

查看:355
本文介绍了ASP.net MVC - 模型绑定排除类字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的一个项目 - 我已经打了一个意想不到的障碍结果。
用简单的公共领域的类(注意不是属性)似乎并不想打的不错
ASP.net MVC 3.0模型粘合剂。

这设计是?

除了修改字段属性 - 这里的任何选项

更新

原因很简单域(而不是属性)是因为我与MVC和脚本夏普项目之间共享库工作。脚本尖锐支持属性 - 但它成为与JavaScript一个烂摊子在视图(使用knockout.js)

因此​​,尽管我很想(这是只使用属性)我使用的公共领域在我的DTO类。

我想,以避免同一类的多个定义。叹息


解决方案

  

这设计是?


是的,只有公共属性的getter / setter方法​​与默认模型联编工作。


  

除了修改字段属性 - 这里的任何选项


这是你应该为你应该使用视图模型反正做什么。和视图模型的意见而设计的。所以改变字段属性是做正确的方式。现在,如果你不遵循良好做法,并尝试使用您的域模型作为输入/输出操作,那么你将不得不编写与领域工作的自定义模型粘合剂。这将是一个大量的工作,但。

In a recent project - i've hit an unexpected snag.
A class with simple public fields (note not properties) doesn't seem to want to play nice with the ASP.net MVC 3.0 model binder.

Is this by design?

besides changing the fields to properties - any options here?

update

The reason for the simple fields (instead of properties) is because I'm working with a shared library between MVC and a Script Sharp project. Script sharp supports properties - but it becomes a mess with javascript in the view (using knockout.js)

So despite what i'd love (which is to just use properties) i'm using public fields in my dto classes.

I wanted to avoid having multiple definitions of the same classes. sigh

解决方案

Is this by design?

Yes, only properties with public getter/setters work with the default model binder.

besides changing the fields to properties - any options here?

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 - 模型绑定排除类字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆