简体   繁体   English

基于ASP.NET MVC的CMS-动态生成表单助手

[英]ASP.NET MVC based CMS - dynamic generation of form helpers

I am working on an ASP.NET MVC based CMS that presents a rather extreme case. 我正在研究一个基于ASP.NET MVC的CMS,它表现出一种极端的情况。 The system must allow the user to add custom content types based on different fields, and for every field, one can add options and validations. 系统必须允许用户基于不同的字段添加自定义内容类型,并且对于每个字段,都可以添加选项和验证。 The thing is that everything is stored in a complex DB and extracted at runtime using LINQ. 事实是,所有内容都存储在复杂的数据库中,并在运行时使用LINQ提取。

I am pretty fresh with ASP>NET MVC so the following dilemma came to mind How should I make the content creation view so that form helpers are not predefined int he view code but are loaded based on the type of the field ? 我对ASP> NET MVC感到很新鲜,因此想到了以下难题:如何创建内容创建视图,以便在视图代码中未预定义表单助手,而是根据字段类型加载表单助手? Do I have to create a factory class that checks the value of the type property of the field, and then returns a helper based on that or there's a better way to do it. 我是否必须创建一个工厂类来检查该字段的type属性的值,然后根据该值返回一个帮助器,或者有更好的方法呢? This one seems pretty rigid to me , because anytime I make a change in the Fieldtypes table, I will have to make sure to create a check for that new type too. 这对我来说似乎很死板,因为每当我在Fieldtypes表中进行更改时,我都必须确保也为该新类型创建一个检查。

public class CType {
  string Name; //e.g Post Article etc
  List<ContentData> data ;
...
}

public class ContentData {
  string Data; // Basically this is the data stored for each field
  FieldInstance fieldInstance; 
...
}

public class FieldInstance {
  string Title; // e.g Title Body etc.

  FieldType Type ; // e.g textbox textarea image checkbox etc
...
}

public class FieldType {
  string Type; // e.g textbox textarea image checkbox etc
...
}

I see an HTML Helper in your future. 我将来会看到HTML Helper。 The HTML Helper can work through your Model at runtime and output the appropriate HTML for the View. HTML Helper可以在运行时遍历您的模型,并为View输出适当的HTML。 If you go that route, I suggest you get to know the StringBuilder and TagBuilder classes. 如果您走这条路线,我建议您了解StringBuilder和TagBuilder类。 They'll simplify things and help make your HTML Helper much more readable. 它们将简化事情并帮助使HTML Helper更具可读性。

I did not know about the concept of templated helpers. 我不知道模板化助手的概念。 This is what happens when you're new to something. 这是当您不熟悉某事物时发生的事情。 Pretty much, this is what fixed my problem 差不多,这就是解决我的问题的原因

http://msdn.microsoft.com/en-us/library/ee308450%28VS.100,printer%29.aspx http://msdn.microsoft.com/en-us/library/ee308450%28VS.100,printer%29.aspx

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

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