.Net mvc 绑定器
我对 Microsoft mvc 框架中的模型绑定程序有不同意见。 我的域模型有一个基类,该基类具有只读的 id 属性和类型 guid。 但因为它是只读的,绑定器无法设置属性的值。 那么我完全搞砸了还是有人可以解决我的问题?
编辑:抱歉,一如既往,有一些我忘记提及的附加信息,我希望活页夹绑定 id 属性的唯一时间是当它是另一个模型对象的相关对象时。 就像在这种情况下,当我为帖子选择了类别并且我只想要 ORM 的 id 时。
I'm having a disagreement with the model binders in Microsofts mvc framework. I have a base class for my domain models that have an id property that is readonly and type guid. But because it's readonly the binders can't set the value of the property. So am I totally screwed or can someone solve my problem?
EDIT: Sorry as always there is additonal information that I forgot to mention, the only time I want the binder to bind the id property is when it's a related object to another model object. Like in this case when I have a chosen a category for a post and I only want the id for the ORM.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
活页夹不能设置只读值——也不应该! 常见的情况是您创建了一个模型,并且它返回一个 ID。
下次检索该对象时,ID 将传递到构造函数中以方便检索该对象。 您在任何时候都不需要双向绑定该属性。
The binders can't set a readonly value - nor should they! The common scenario is that you've created a model, and it returns an ID.
The next time you retrieve that object, the ID is passed into the constructor to facilitate retrieving the object. At no point do you need to two-way bind that property.
我不同意答案,-为什么活页夹不能设置只读或私有属性? 这引入了限制并防止对象不可变。 (您当然可以实现自己的模型绑定器)
我不是绑定器或反射方面的专家,但我猜测标准实现使用反射,并且反射不会设置不设置私有属性的限制。
I don't agree with the answer, -Why shouldn't the binder be able to set readonly or private properties? This introduces a limitation and prevents the objects to be immutable. (You could of course implement an own modelbinder)
I'm not an expert on binders or reflection but I would guess that the standard implementation uses reflection and reflection don't set a limitation to not setting private properties.