简体   繁体   English

MVC4在运行时构建视图

[英]MVC4 building views at runtime

I have written a WPF desktop app where the layout of the view(s) is generated at runtime based on a configuration in a database. 我编写了一个WPF桌面应用程序,其中,运行时根据数据库中的配置生成视图的布局。 A new requirement has arisen where they want to expose the same layouts via the web. 当他们想通过网络公开相同的布局时,出现了新的要求。

As far as I can see I have two options 据我所知,我有两个选择

  • Generate the MVC view at runtime 在运行时生成MVC视图
  • Code the layouts in separate views and bind the data through the normal model binding mechanism. 在单独的视图中编码布局,并通过常规模型绑定机制绑定数据。

I can see the first option could be quite complex (I'm all for simplicity) and I'm not sure how the model binding would work. 我可以看到第一个选项可能非常复杂(为简单起见,我全都这样做了),而且我不确定模型绑定将如何工作。 The second option will be much simpler but will result in much more "boiler plate" code that needs to be enhanced \\ maintained. 第二种选择会简单得多,但是会导致更多的“样板代码”需要增强\\维护。

Does anyone have any previous experience with generating Razor views at runtime or is this something that I should avoid? 是否有人在运行时生成Razor视图有过任何经验,还是应该避免这种事情?

This is do-able via a mix of T4 templates and run-time compilation -- but I'm not sure that I would recommend it. 这可以通过混合使用T4模板和运行时编译来实现-但我不确定我会推荐这样做。 Another route that I would recommend would be to generate JavaScript templates on the server-side that could be rendered on the client. 我建议的另一种方法是在服务器端生成可在客户端上呈现的JavaScript模板。 There is a wide variety of templating libraries out there. 有一个广泛的 各种模板库在那里。 Because you are using MVC, it would not be that difficult to treat your actions within the controllers as endpoints that you could call asynchronously. 因为您使用的是MVC,所以将控制器内的操作视为可以异步调用的端点并不难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM