Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

1 solution

When returning the data to the your View you can use razor syntax on the view to generate the specific control you want


@model YourViewModel

@if(Model.YourControlId == 1){
else if(Model.YourControlId == 2){
   @Html.DropDownListFor(x=> x.YourPropertyBinding, listOfData)
else if(Model.YourControlId == 3){
Share this answer
Member 11174491 29-Oct-14 4:19am    
Hi, Thank you for your response. I already tried this way, but it is throwing below error

"CS1061: 'System.Web.Mvc.HtmlHelper<mvcdemo.models.profile>' does not contain a definition for 'DropDownlistFor' and no extension method 'DropDownlistFor' accepting a first argument of type 'System.Web.Mvc.HtmlHelper<mvcdemo.models.profile>' could be found (are you missing a using directive or an assembly reference?)"

Can you please correct me in what place i am doing mistake?

I post my code here:

@model mvcdemo.Models.profile

ViewBag.Title = "dynamicdisplay";

@if(Model.control_id == 1){
else if(Model.control_id == 2){
@Html.DropDownlistFor(x=> x.profile_filed,"select")
else if(Model.control_id == 3){
@Html.CheckBoxFor(x=> x.profile_filed)

Samer Aburabie 29-Oct-14 4:23am    
My friend .. my code might have typos .. I am posting code you need to check on the IDE you are using, so you will be able to understand before applying it.

I suspect that DropDowlistFor should be DropDownListFor (capital l for list) and you need to pass the list of data to this method too, I am pointing the general idea here since I dont know your data source and didn't see your code.
Member 11174491 29-Oct-14 8:06am    
Hi, Thank you for pointing out me the mistake.

I tried below code:

@if(Model.control_id == 1){
else if(Model.control_id == 2){
@Html.DropDownListFor(x=> x.profile_filed,@ViewBag.control as SelectList,"select")
else if (Model.control_id == 3)
@Html.CheckBoxFor(x => x.isselected)
@Html.DisplayFor(x => x.profile_filed)


Now, I am getting NullReferenceException.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900