将动态下拉字段添加到Razor视图

时间:2018-09-08 09:07:32

标签: c# asp.net-mvc razor dynamicform

我正在基于具有这种结构的模型构建表单

public class DynamicFormElementsModel
{
    //param might be shown in UI , but will not pass while setting params for report . Example select org.  
    public bool ReportParameter { get; set; }

    public string  ParamName { get; set; }

    public string ParamLabel { get; set; }

    public string ParamType { get; set; }

    public bool Visible { get; set; }

    public string ParamValue { get; set;}
}

在Razor中,我正在构建类似

的控件
 @using (Html.BeginForm("Index", "Reports", FormMethod.Post, new { role = "form" }))
        {
            @Html.HiddenFor(p => Model.ReportId)
            for (int i = 0; i < Model.Parameters.Count(); i++)
            {
                <div class="form-group">
                    <label class="control-label">@Model.Parameters[i].ParamLabel</label>

                    @switch (@Model.Parameters[i].ParamType.ToLower())
                    {
                        case "textbox":
                            if (@Model.Parameters[i].Visible)
                            {
                                <input type="text" class="form-control" value="@Model.Parameters[i].ParamValue" name="@Model.Parameters[i].ParamName" />
                            }
                            else
                            {
                                <input type="hidden" value="@Model.Parameters[i].ParamValue" name="@Model.Parameters[i].ParamName" />
                            }
                            break;
                        default:
                            break;
                    }
                </div>
            }
                <input type="submit" class="btn btn-default" value="Submit" />
        }

使用此方法可以正常显示普通文本框。但是我该如何做一个下拉列表,并从一个表id,sql表中的名称值对填充该下拉列表

0 个答案:

没有答案
相关问题