多态MVC视图 [英] Polymorphic MVC Views

查看:86
本文介绍了多态MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在做ASP.NET MVC的注册页面,那里有它的变化取决于多个条件字段列表(取自数据库)。

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).

你会考虑在没有code一个视图中显示这些领域的最佳方法每次数据库更新时的变化。

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

目前的制度,我想正在以名称,数据类型,然后刚刚经历列表遍历一个字典,并把每一个名字的一个新的输入型(使用数据类型的交换机,以获得正确的编辑器)。但是,这似乎是一个老式的,而不是充分利用.NET MVC系统的好处(如验证,默认值,自定义编辑特定的内容)。

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).

有没有办法也许我能在.NET不知道来处理这个问题?

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

推荐答案

我有非常类似的问题。发帖是<一个href=\"http://stackoverflow.com/questions/11832044/asp-net-mvc3-razor-syntax-to-cast-a-model-to-its-subclass-by-reflection\">here.

I had very similar problem. Posting is here.

没有,就在ASP.NET MVC中没有多态性的意见。至少我不能使它工作在Stack Overflow上做吨,吨试验,并问这个问题以不同的方式多次。

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.

该型在视图中定义 @model 将确定其类型的视图中,而不是实际的模型的类型(如果是接口或抽象类或基类,当你在子类中传递与所有漂亮的装饰品,这是行不通的,因为类型是由模型类型定义)。

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).

对不起,没有正面回答你的问题。我最后写一堆编辑模板,这是唯一由内而外模型类型不同。巨大的干违规,但这是ASP方式工作MVC,的可惜

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.

这篇关于多态MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆