asp.net mvc strongly typed helpers - should your render binding object be the same as your posting object?

前端 未结 7 1370
刺人心
刺人心 2021-01-02 22:52

i see that asp.net mvc 2 has strongly typed helped and looking initially at the way it works i think maybe i am doing something wrong in asp.net mvc 1 in terms of data bindi

相关标签:
7条回答
  • 2021-01-02 23:44

    I use separate view models for input [GET] and output [POST] as well, unless the two models are identical. IMHO this approach is cleaner, easier to maintain, and more clearly expresses which data will be updated by a given controller action.

    There's a cost in terms of LOC, but the extra code is usually not logic. In most cases, I don't feel that duplicating some automatic properties on two objects violates the DRY principle, and I think the cost is justified to follow SRP.

    The other cost, as you've noted, is that the built-in strongly typed helpers don't work so well. Have you thought about writing your own helpers that support your pattern? For instance, you might overload one of the existing helpers so that you can specify both the source (e.g. the property of the input model) and the destination (e.g. the name of the form field that is posted to the output model).

    0 讨论(0)
提交回复
热议问题