首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.NET MVC :如何在运行时根据存储在数据库表中的html细节动态地创建razor视图表单

ASP.NET MVC :如何在运行时根据存储在数据库表中的html细节动态地创建razor视图表单
EN

Stack Overflow用户
提问于 2019-06-10 00:47:25
回答 1查看 262关注 0票数 -3

使用data的表结构的屏幕截图

表结构如下:

代码语言:javascript
复制
Id | Category | DisplayName | FieldName | FieldType | FieldLength | IsRequired

详情请参见截图。

在选择类别时,应在视图中填充表中针对该类别定义的所有HTML字段。

以及如何在表单提交时验证。

我有400多个类别。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-10 04:23:55

这就是我认为你能做的。

首先声明一个任意名称的class,我将使用SampleClass

代码语言:javascript
复制
public class SampleClass
{
    public int Id { get; set; }
    public string Category { get; set; }
    public string DisplayName { get; set; }
    public string FieldName { get; set; }
    public string FieldType { get; set; }
    public string FieldLength { get; set; }
    public int IsRequired { get; set; }//you can convert this to boolean to make it easier
}

其次,从控制器中的db填充这个类

代码语言:javascript
复制
//assuming you are using linq query
var allEntry = context.Table.Where(m => m.Category == SelectedCategory);
var allViews = new List<SampleClass>();
foreach (var entry in allEntry)
{
    allViews.Add(new SampleClass
    {
        FieldName = entry.FieldName,
        //and so on...

    });
}

第三,在视图中,按照我在评论中所说的那样做

代码语言:javascript
复制
@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { @class = "forms-horizontal", role = "form" }))
{
    <div class="form-horizontal">
        <h4>Title of the form</h4>
        <hr /><br />
        @foreach (var a in List<SampleClass>)//note this(List<SampleClass>) will be passed from the controller to the view.
        {
            switch (a.FieldType)
             {
                case "Test":
                    <div class="form-group">
                        <label for="@a.FieldName"></label>
                        <input id="@a.FieldName" type="text" name="@a.FieldName" required="@(a.IsRequired == 1 ? "required" : "")" />
                    </div>
                    break;
                case "Number":
                    <div class="form-group">
                        <label for="@a.FieldName"></label>
                        <input id="@a.FieldName" type="number" name="@a.FieldName" required="@(a.IsRequired == 1 ? "required" : "")" />
                    </div>
                    break;
                case "Select":
                    <div class="form-group">
                        <label for="@a.FieldName"></label>
                        @Html.DropDownList(a.FieldName, new SelectList(DbUtils.GetEvaluations(), "Value", "Text", null), "Select..", new { @required = @a.IsRequired == 1 ? "required" : "" })
                    </div>
                    break;
            }
        }
        <div class="form-group">
            <div class="col-md-offset-3 col-md-9">
                <input type="submit" value="Submit" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

根据需要,您可以在select子句中使用更多的cases

希望这能帮上忙..

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56516517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档