In my database, i have a column called control_id which consists value like 1,2,3,4. Based on this value, i have to generate controls like Text box, Dropdownlist and Check box. (For example, if my control_id is 1, has to generate Text box, and for 2, dropdownlist and so on) I am completely new to MVC. Can anyone point me in right direction to implement this scenario?
Asked
Active
Viewed 7,313 times
1 Answers
6
Create an enum for control types
public enum ControlTypes { TextBox = 1, Dropdown = 2, CheckBox = 3, Label = 4 // Define other control types }
Create Baseclass for handling all 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 }
Create a View Model for each control, now for textbox
// Textbox deriving class public class TextBoxViewModel : DynamicControlsBase { public override string FieldLabel { get { return base.FieldLabel; } set { base.FieldLabel = value; } } public override List<SelectListItem> ValueList { get { return base.ValueList; } set { base.ValueList = value; } } //View model for Label field public class LabelViewModel : DynamicControlsBase { public override string FieldLabel { get; set; } }
Create a View Model for each control, here for dropdown.
public class DropdownViewModel : DynamicControlsBase { public override string FieldLabel { get; set; } public override List<SelectListItem> ValueList { get { return base.ValueList; } set { base.ValueList = value; } } }
Now create a folder named 'EditorTemplates' under Views>>Shared.
In that folder create a view for each view model (Textbox and dropdown and more) with the same name as the ViewModel ex. TextBoxViewModel.cshtml, and do the same for dropdown and other controls.
@model MVCSample.Models.TextBoxViewModel @Html.TextBoxFor(m => m.ControlValue)
In the controller action method assign the value for each respective view model like below and pass it to the view.
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" } } };
In the actual view, render the controls using html helper 'EditorFor',
@Html.EditorFor(m => m.DynamicControlsBase)
In controller modify the GetControl method as,
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();
}

Paul Zahra
- 9,522
- 8
- 54
- 76

Venkatesh
- 1,204
- 1
- 10
- 18
-
Ya yes. I tried that only. But getting Label_field for all control. In Enum class, for 1,2,3 you defined control class. But for label how do define that?. I am being struggled in bringing corresponding label_field for every conrol. Profile_field column has to be converted into label with same name as column name for respective control. Please give me right direction once again. – user3793029 Nov 13 '14 at 12:45
-
I misunderstood your request earlier as I was thinking you want the label to display for each controle, if I'm right now I'm placing the code for adding a new label field as like other controls. Please see the updated answer. – Venkatesh Nov 13 '14 at 13:15
-
Getting no result. Simply displaying controls. – user3793029 Nov 14 '14 at 06:23
-
@Html.LabelFor(m=>m.FieldLabel). i used this code in LabelViewModel.cshtml – user3793029 Nov 14 '14 at 06:24
-
Use @Html.DisplayFor(m=>m.FieldLabel) for showing field label and for label control value @Html.DisplayFor(m=>m.ControlValue) as like used in other editor templates. – Venkatesh Nov 14 '14 at 06:26
-
I used DisplayFor, but same result. and In your example code, textbox and dropdown all are used @Html.TextboxFor(m=>m.ControlValue) and DropndownListFor(m=>m.ControlValue) – user3793029 Nov 14 '14 at 06:33
-
Ok, but try and debug your code since I've tried out getting the correct result and its much simpler to add controls if you start find the way how it is implemented. So take some time to better under the code the way it is implemented to make it ease in modifying. – Venkatesh Nov 14 '14 at 06:41
-
Finally we got the result as expected. Always thankful to you for helped us in developing dynamic control generation module. Appreciate your intention of helping to others. – user3793029 Nov 24 '14 at 10:07