asp.net mvc 4 - Control creation based on model value in MVC4 -



asp.net mvc 4 - Control creation based on model value in MVC4 -

in database, have column called control_id consists value 1,2,3,4. based on value, have generate controls text box, dropdownlist , check box. (for example, if control_id 1, has generate text box, , 2, dropdownlist , on) new mvc. can point me in right direction implement scenario?

create enum command types public enum controltypes { textbox = 1, dropdown = 2, checkbox = 3, label = 4 // define other command types } create baseclass handling command types. public class dynamiccontrolsbase { public virtual string fieldlabel { get; set; } public string controlvalue { get; set; } // dropdown public virtual list<selectlistitem> valuelist { get; set; } // likewise implement other required property command uses } create view model each control, textbox // textbox deriving class public class textboxviewmodel : dynamiccontrolsbase { public override string fieldlabel { { homecoming base.fieldlabel; } set { base.fieldlabel = value; } } public override list<selectlistitem> valuelist { { homecoming base.valuelist; } set { base.valuelist = value; } } //view model label field public class labelviewmodel : dynamiccontrolsbase { public override string fieldlabel { get; set; } } create view model each control, here dropdown. public class dropdownviewmodel : dynamiccontrolsbase { public override string fieldlabel { get; set; } public override list<selectlistitem> valuelist { { homecoming base.valuelist; } set { base.valuelist = value; } } } now create folder 'edittemplates' under views>>shared. in create view each view model (textbox , dropdown , more) same name viewmodel ex. textboxviewmodel.cshtml same dropdown , other controls. @model mvcsample.models.textboxviewmodel @html.textboxfor(m => m.controlvalue) in controller action method assign value respective view model below , pass 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 actual view, render controls using html helper 'editorfor', @html.editorfor(m => m.dynamiccontrolsbase)

in controller modify getcontrol method as,

private dynamiccontrolsbase getcontrol(string controltype, datatable tabledata = null, list<selectlistitem> controlvalue = null) { if (controltype.equals(convert.tostring((int)controltypes.textbox))) { homecoming 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))) { homecoming 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))) { homecoming 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() }; } homecoming new dynamiccontrolsbase(); }

asp.net-mvc-4

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -