Follwing convention(s) are given. Each Action
has a single parameter of type BaseRequest
with data depending on the Action
. The ViewModel
is always of type BaseResponse
.
What I'm trying to do is, that if a View
contains a form, the POST
-Action requires some sort of BaseRequest
.
How can I achieve correct-model binding as of the ViewModel
is BaseResponse
?
I already tried to add a property of XYZRequest
in XYZResponse
so I could bind like
@Html.ChecBoxFor(m => m.RequestObject.SomeBooleanProperty)
but this will generate name
RequestObject.SomeBooleanProperty which will not bind correctly to the POST-Action which accepts the XYZRequest
.
Is there something completely wrong with this kind of conventions or am I missing something?
Update #1
I also tried creating a new temporary
object of type XYZRequest
and bind to it like
@Html.CheckBoxFor(m = tmpReq.SomeBooleanProperty)
which will render a name
of tmp.SomeBooleanProperty
which also could not be bound.
Update #2 - additional information
Following structure is set.
BaseRequest
isabstract
GetOverviewRequest
:BaseRequest
GetOverviewRequest
has properties of typestring
,int
or any other complex type and evenLists
orDictionaries
If the GetOverviewResponse
, which inherits from BaseResponse
, is given back to the View
and provides a property named TheProperty
of type GetOverviewRequest
binding fails as of
@Html.TextBoxFor(m => m.TheProperty.SomeBooleanValue)
will try to bind to TheProperty
-property in the GetOverviewRequest
object, which just not exists.
This may would work if GetOverviewRequest
has a property called TheProperty
to bind. But if it is named differently, binding will also fail.
I just want something like
<input name="SomeBooleanValue">
<input name="SomeComplexType.SomeStringValue">
instead of
<input name="TheProperty.SomeBooleanValue">
<input name="TheProperty.SomeComplexType.SomeStringValue">
Update #3 - added sample project
Sample project via dropbox.com
Update #4 - explanation, why solution from @StephenMuecke is not working
As mentioned in comments, the solution in other question needs to know the name of the property in the GetOverviewResponse
-object. The property is named TheProperty
, therefore I have to add [Bind(Prefix = "TheProperty)]
to enable correct binding. I really don't like magic strings. And "TheProperty"
is a magic string. If one changes the name of the TheProperty
to RenamedProperty
, the whole binding will fail.
So. I'm now looking for a way to set the prefix some-kind dynamically.
[Bind(Prefix = GetOverviewResponse.NameOf(m => m.TheProperty))]
would be really awesome. Maybe some kind of a custom attribute? As of BindAttribute is sealed, there is no chance to create one inherited from this.
Any ideas?