Asp.net mvc 4 MVC4中基于模型值的控件创建

Asp.net mvc 4 MVC4中基于模型值的控件创建,asp.net-mvc-4,Asp.net Mvc 4,在我的数据库中,我有一个名为control_id的列,它由1,2,3,4这样的值组成。基于这个值,我必须生成文本框、下拉列表和复选框等控件。 (例如,如果我的控件id为1,则必须生成文本框,对于2,则为dropdownlist,依此类推)我对MVC是完全陌生的。有人能给我指出实现这个场景的正确方向吗 为控件类型创建枚举 public enum ControlTypes { TextBox = 1, Dropdown = 2, CheckBox = 3, La

在我的数据库中,我有一个名为control_id的列,它由1,2,3,4这样的值组成。基于这个值,我必须生成文本框、下拉列表和复选框等控件。 (例如,如果我的控件id为1,则必须生成文本框,对于2,则为dropdownlist,依此类推)我对MVC是完全陌生的。有人能给我指出实现这个场景的正确方向吗

  • 为控件类型创建枚举

    public enum ControlTypes  
    {
        TextBox  = 1,
        Dropdown = 2,
        CheckBox = 3,
        Label = 4
        // Define other control types  
    }
    
  • 创建用于处理所有控件类型的基类

    public class DynamicControlsBase
    {
        public virtual string FieldLabel { get; set; }
    
        public string ControlValue { get; set; }
        // For dropdown
        public virtual List<SelectListItem> ValueList { get; set; }
    
        // Likewise implement the other required property that the control uses
    }
    
  • 在控制器操作方法中,为每个相应的视图模型指定值,如下所示,并将其传递给视图

    DynamicControlsBase dcb = new TextBoxViewModel
    {
        ControlValue = "Test"
    };
    
    dcb = new DropdownViewModel
    {
         ControlValue = "Test",
         ValueList = new List<SelectListItem>
         {
              new SelectListItem
              {
                   Text = "Test",
                   Value= "1"
              },
              new SelectListItem
              {
                   Text = "Text",
                   Value= "1"
              }
         }
    };
    
  • 在控制器中,将GetControl方法修改为

    private DynamicControlsBase GetControl(string controlType, DataTable tableData = null, List<SelectListItem> controlValue = null)
    {
        if (controlType.Equals(Convert.ToString((int)ControlTypes.TextBox)))
        {
            return new TextBoxViewModel
            {
                ControlValue = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("Value")).FirstOrDefault(),
                FieldLabel = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("FieldLabel")).FirstOrDefault()
            };
        }
        else if (controlType.Equals(Convert.ToString((int)ControlTypes.Dropdown)))
        {
            return new DropdownViewModel
            {
                ControlValue = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("Value")).FirstOrDefault(),
                FieldLabel = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("FieldLabel")).FirstOrDefault(),
                ValueList = controlValue
            };
        }
        if (controlType.Equals(Convert.ToString((int)ControlTypes.Label)))
        {
            return new LabelViewModel
            {
                ControlValue = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("Value")).FirstOrDefault(),
                FieldLabel = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("FieldLabel")).FirstOrDefault()
            };
        }
    
        return new DynamicControlsBase();
    }
    
    专用DynamicControlsBase GetControl(字符串控制类型,DataTable tableData=null,列表控制值=null)
    {
    if(controlType.Equals(Convert.ToString((int)ControlTypes.TextBox)))
    {
    返回新的TextBoxViewModel
    {
    ControlValue=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“Value”))。FirstOrDefault(),
    FieldLabel=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“FieldLabel”)。FirstOrDefault()
    };
    }
    else if(controlType.Equals(Convert.ToString((int)ControlTypes.Dropdown)))
    {
    返回新的DropdownViewModel
    {
    ControlValue=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“Value”))。FirstOrDefault(),
    FieldLabel=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“FieldLabel”)。FirstOrDefault(),
    ValueList=controlValue
    };
    }
    if(controlType.Equals(Convert.ToString((int)ControlTypes.Label)))
    {
    返回新的LabelViewModel
    {
    ControlValue=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“Value”))。FirstOrDefault(),
    FieldLabel=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“FieldLabel”)。FirstOrDefault()
    };
    }
    返回新的DynamicControlsBase();
    }
    
  • 为控件类型创建枚举

    public enum ControlTypes  
    {
        TextBox  = 1,
        Dropdown = 2,
        CheckBox = 3,
        Label = 4
        // Define other control types  
    }
    
  • 创建用于处理所有控件类型的基类

    public class DynamicControlsBase
    {
        public virtual string FieldLabel { get; set; }
    
        public string ControlValue { get; set; }
        // For dropdown
        public virtual List<SelectListItem> ValueList { get; set; }
    
        // Likewise implement the other required property that the control uses
    }
    
  • 在控制器操作方法中,为每个相应的视图模型指定值,如下所示,并将其传递给视图

    DynamicControlsBase dcb = new TextBoxViewModel
    {
        ControlValue = "Test"
    };
    
    dcb = new DropdownViewModel
    {
         ControlValue = "Test",
         ValueList = new List<SelectListItem>
         {
              new SelectListItem
              {
                   Text = "Test",
                   Value= "1"
              },
              new SelectListItem
              {
                   Text = "Text",
                   Value= "1"
              }
         }
    };
    
  • 在控制器中,将GetControl方法修改为

    private DynamicControlsBase GetControl(string controlType, DataTable tableData = null, List<SelectListItem> controlValue = null)
    {
        if (controlType.Equals(Convert.ToString((int)ControlTypes.TextBox)))
        {
            return new TextBoxViewModel
            {
                ControlValue = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("Value")).FirstOrDefault(),
                FieldLabel = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("FieldLabel")).FirstOrDefault()
            };
        }
        else if (controlType.Equals(Convert.ToString((int)ControlTypes.Dropdown)))
        {
            return new DropdownViewModel
            {
                ControlValue = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("Value")).FirstOrDefault(),
                FieldLabel = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("FieldLabel")).FirstOrDefault(),
                ValueList = controlValue
            };
        }
        if (controlType.Equals(Convert.ToString((int)ControlTypes.Label)))
        {
            return new LabelViewModel
            {
                ControlValue = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("Value")).FirstOrDefault(),
                FieldLabel = tableData.AsEnumerable().Where(a => a.Field<int>("Control_id").Equals(int.Parse(controlType))).Select(a => a.Field<string>("FieldLabel")).FirstOrDefault()
            };
        }
    
        return new DynamicControlsBase();
    }
    
    专用DynamicControlsBase GetControl(字符串控制类型,DataTable tableData=null,列表控制值=null)
    {
    if(controlType.Equals(Convert.ToString((int)ControlTypes.TextBox)))
    {
    返回新的TextBoxViewModel
    {
    ControlValue=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“Value”))。FirstOrDefault(),
    FieldLabel=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“FieldLabel”)。FirstOrDefault()
    };
    }
    else if(controlType.Equals(Convert.ToString((int)ControlTypes.Dropdown)))
    {
    返回新的DropdownViewModel
    {
    ControlValue=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“Value”))。FirstOrDefault(),
    FieldLabel=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“FieldLabel”)。FirstOrDefault(),
    ValueList=controlValue
    };
    }
    if(controlType.Equals(Convert.ToString((int)ControlTypes.Label)))
    {
    返回新的LabelViewModel
    {
    ControlValue=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“Value”))。FirstOrDefault(),
    FieldLabel=tableData.AsEnumerable()。其中(a=>a.Field(“Control_id”).Equals(int.Parse(controlType)))。选择(a=>a.Field(“FieldLabel”)。FirstOrDefault()
    };
    }
    返回新的DynamicControlsBase();
    }
    
    Ya是的。我只是试了一下。但要为所有控件获取Label_字段。在Enum类中,为1,2,3定义了控制类。但对于标签,如何定义它呢?。我正在努力为每个控制带来相应的标签字段。Profile_字段列必须转换为与相应控件的列名同名的标签。请再次给我正确的方向。我之前误解了您的请求,因为我认为您希望为每个控件显示标签。如果我现在正在放置添加新标签字段的代码,就像其他控件一样。请查看更新的答案。未得到任何结果。只显示控件。@Html.LabelFor(m=>m.FieldLabel)。我在LabelViewModel.cshtmlUse@Html.DisplayFor(m=>m.FieldLabel)中使用了这段代码,用于显示字段标签和标签控制值@Html.DisplayFor(m=>m.ControlValue),就像在其他编辑器模板中使用的一样。是的。我只是试了一下。但要为所有控件获取Label_字段。在Enum类中,为1,2,3定义了控制类。但对于标签,如何定义它呢?。我正在努力为每个控制带来相应的标签字段。Profile_字段列必须转换为与相应控件的列名同名的标签。请再次给我正确的方向。我之前误解了您的请求,因为我认为您希望为每个控件显示标签。如果我现在正在放置添加新标签字段的代码,就像其他控件一样。请查看更新的答案。未得到任何结果。只显示控件。@Html.LabelFor(m=>m.FieldLabel)。我在LabelViewModel.cshtmlUse@Html.DisplayFor(m=>m.FieldLabel)中使用了这段代码来显示字段标签和标签控件值@Ht