在MVC中从数据库生成表单

时间:2016-06-28 03:48:17

标签: sql-server asp.net-mvc asp.net-mvc-4 razor

我正在尝试从我的数据库生成表单。

CREATE TABLE [dbo].[FieldsMaster](
    [FieldId] [int] IDENTITY(1,1) NOT NULL,
    [FieldControlName] [varchar](50) NULL,
    [FieldDataType] [varchar](20) NULL,
    [FieldControlType] [varchar](30) NULL,
    [DisplayLabel] [varchar](200) NULL,
    [FieldSize] [int] NULL,
    [FieldRegularExpression] [varchar](200) NULL,
    [FieldOptions] [varchar](500) NULL,
    [OptionalQuery] [varchar](200) NULL,
    [REMessage] [varchar](150) NULL,
    [FieldRequired] [bit] NULL,
    [FRMessage] [varchar](150) NULL  
)

为表格生成一些数据

insert into FieldsMaster (FieldControlName,FieldDataType,FieldControlType,DisplayLabel,FieldSize,FieldRegularExpression,
FieldOptions,OptionalQuery,REMessage,FieldRequired,FRMessage,IsActive)
values
('txtOtherInfo','varchar','TextArea','Other Information',400,NULL,
NULL,NULL,NULL,0,NULL)

insert into FieldsMaster (FieldControlName,FieldDataType,FieldControlType,DisplayLabel,FieldSize,FieldRegularExpression,
FieldOptions,OptionalQuery,REMessage,FieldRequired,FRMessage,IsActive)
values
('txtTitle','varchar','TextBox','Title',100,NUll,
NULL,NULL,NULL,1,'Please enter title')

现在这个带有字段的表单存储在我的sql db中。我想在mvc中生成一个表单。如何形成模型/如何在视图中生成表单? 我真的很困惑。非常感谢您的帮助。感谢

要了解更多信息,请根据FieldsMaster

中的字段显示以下表单
Other Information(as label) : TextArea (With Id as txtOtherInfo)
Title (as Label)            : TextBox (With Id as txtTitle)

1 个答案:

答案 0 :(得分:1)

如果您想直接从数据库添加表单,可以这样做。

首先,你需要做一个像这样的Enum模型(用于电子邮件):

    public int EmailTemplateId { get; set; }
    public int Type { get; set; }
    public string Name { get; set; }    

public enum Emailtype
{
    Base = 0,
    Recovery = 1,
    NewAccount = 2,
    ChangePassword = 3,
    BlockAccount = 4,
    UnlockAccount = 5,
    ForgetPassword = 6
  }

因此,每个人都代表一个不同的模板,在模板视图中,您有类似

的内容
<body>
//some code of your template
{-container-}
</body>

如您所见{-container-}是您为每个模板更改的属性。所以你制作了新的容器,你可以插入到像

这样的sql中
<tr>
 <td>Hi {name}. Your password is {password}</td>
</tr>

您不需要在此处添加标记,因为您已将其放入第一个模板和此代码

在控制器中:

   public void NewAccount(string name, string email,string password, string user)
    {

        var from = model.From;
        var loginUrl = model.LoginUrl;
        const string title = "New Register - Stackoverflow";

           EmailsService sendEmail = new EmailsService
        {
            Subject = title,
            To = email,
            From = from
        };
}

          //There is our first template (Base template)
        var templateBase = Get(Emailtype.Base);
          //This one is template who changes (container)
        var templateText = Get(Emailtype.NewAccount);

     //Here you replace Name property of class for your container code
           var body = templateBase.Name.Replace("{-container-}", templateText.Name);


           sendEmail.EmailBody = body.Replace("{Name}", name)
            .Replace("{Title}", title)
            .Replace("{User}", user)
            .Replace("{Password}", password)
            .Replace("{Link}", loginUrl);
        sendEmail.Send();
    }    

最后在你的sql中你可以插入容器的代码......它显示如下:

EmailTemplateId=1
Type= 1 //(Recovery ones)
Name=    <tr> <td>Hi {name}. Your password is {password}</td> </tr>

希望它有所帮助!