views:

147

answers:

2

Hello

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.

+1  A: 

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.

Peter J
+1  A: 

Hi

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.

Jeep