开发者

MVC3 - Conditional View Rendering

I have a conditional business logic that determines whether a property from a model开发者_JS百科 should be displayed in a view. according to best practices where would be the place to implement it?

Implementing this logic in the view level does not seem right to me.

Thanks


IMO, it is belonged to the Model. I would put that business logic in IsRequiredYourProperyName property in the model.


Really? I would have thought that would be fine in the view provided you're passing the boolean indicating whether or not it should be displayed as part of the ViewModel. The view shouldn't be querying an outside resource to see if it should render certain UI elements, but if everything it needs to determine what to render is in the ViewModel, what's wrong with a simple if{} statement? Alternatively if a conditional display property is common you could create a custom DisplayTemplate or EditorTemplate and for it and implement the logic there.

So your ViewModel should wrap everything you want to send to the view. in your case it sounds like it should wrap your DomainModel and some sort of dictionary or KeyValuePair collection detailing if each property should be displayed or not as booleans. That's what I would do anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜