Polymorphic MVC Views

情到浓时终转凉″ 提交于 2019-12-22 06:43:05

问题


So I am making a registration page in ASP.NET MVC and theres a list of fields which changes depending on a number of conditions (taken from a database).

What would be considered the best approach for displaying these fields on a view without code changes each time the DB is updated.

The current system I am thinking about is making a Dictionary with "Name, DataType" then just iterating through the list and putting a new input type of each name (using a switch for data type to get the correct editor). But it seems like a old fashioned and not fully utilizing the benefits of the .NET MVC system (such as validation, default values, customized editors for specific content).

Is there a approach I may possibly be not aware of in .NET to handle this problem?


回答1:


I had very similar problem. Posting is here.

No, there is no polymorphism views in ASP.NET MVC. At least I couldn't make it work doing tons and tons of experimentation and asking this question several times in different ways on Stack Overflow.

The type of @model defined in the view will define its type inside the view, rather than actual type of the model (in case it was interface or abstract class or base class, while you passing in child class with all your beautiful decorations, which won't work, since type is defined by model type).

Sorry, no positive answer to your question. I ended up writing bunch of editor templates, which were only different by model type inside. Huge DRY violation, but this is the way ASP MVC worked, unfortunately.

Hope this helps saving you some time.



来源:https://stackoverflow.com/questions/12147167/polymorphic-mvc-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!