2

In my database, i have a field called controls which contains text box, dropdownlist, radiobuttonlist etc. My goal is to create controls dynamically based on value of control-field in table. Anybody please help me how to create the controls dynamically in MVC 4 for this scenario.

user3793029
  • 135
  • 3
  • 13
  • 1
    Possible duplicate of [Control creation based on model value in MVC4](http://stackoverflow.com/questions/26624270/control-creation-based-on-model-value-in-mvc4) – Umar Abbas Apr 20 '16 at 07:20
  • here is sample code and also the demo page for creating dynamic controls in mvc.. hope this helps.. [Create dynamic controls in MVC](http://codeanalyze.com/Articles/Details/20184/Add-and-remove-dynamic-textbox-control-to-view-in-aspnet-mvc-with-demo-page) – Jeff D Jan 19 '17 at 17:11
  • Possible duplicate of [SimpleMembership with custom database schema in ASP.NET MVC 4](https://stackoverflow.com/questions/12360126/simplemembership-with-custom-database-schema-in-asp-net-mvc-4) – Ashish Ratan Mar 09 '18 at 11:21

2 Answers2

2

I used steps as mentioned in the below link and got an answer. Hope this will help someone.

Control creation based on model value in MVC4

Community
  • 1
  • 1
user3793029
  • 135
  • 3
  • 13
0

-Create a template in Views/Shared/EditorTemplates for each control (textbox template below).

@model object

@Html.TextBoxFor(m=>m);

-Pass the template name into the Html.EditorFor helper method.

@Html.EditorFor(Function(x)=>x.Password,"templateName");

http://msdn.microsoft.com/en-us/library/ff406475(v=vs.118).aspx

Chris Perry
  • 121
  • 5
  • Thanks for your response. I try to implement your idea and update you the status. – user3793029 Oct 23 '14 at 10:48
  • Can you please provide me some basic example? – user3793029 Oct 23 '14 at 10:53
  • I am having another question too...Instead of control name, if i use control_id, then how to generate control dynamically based on this control_id stored in table. My Scenario is, user will have the option to select control type for their fieldname which is unknown others. After selecting control type( textbox,radiolist, etc...), corresponding control_id will be stored in table. Now based on the control_id stored in table, have to generate control dynamically...I am completely new to mvc. Can you please guide me in right direction to implement it? – user3793029 Oct 23 '14 at 11:31
  • If my control_id is 1, it has to generate textbox and for 2, dropdownlist and for 3,4 accordingly. How to check these condition in controller action method.. – user3793029 Oct 23 '14 at 11:33
  • I am guessing you are trying to allow the user to define there own tables and now you want them to be able to add data? What is the use case? – Chris Perry Oct 23 '14 at 11:36
  • This is completely for profile update as per their wish – user3793029 Oct 23 '14 at 11:37
  • admin only do all these thing, other people doesn't know about type of control selected. – user3793029 Oct 23 '14 at 11:38
  • Sounds like you need a pretty in depth example on how to generate dynamic page in mvc. I have a codeplex project that would probably help (https://dynamicmvc.codeplex.com/). Dynamic MVC does not currently allow you to specify the templates from a table, it assumes a developer would be able to specify this at design time. However, looking at the code may help you. It would also probably be helpful for you to see how the dynamic dropdown editor is implemented since you will need to get the data from somewhere. If you have questions please post in the discussion section on the site. – Chris Perry Oct 23 '14 at 11:45
  • Could you please provide a more detailed description? – nldev May 30 '16 at 13:58