Click here to Skip to main content
15,885,760 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I am working on an MVC application. I want to generate HTML controls dynamically based on user input. something like

if userInput = 3 (Generate three rows, which these controls)

for (i=0; i<3; i++)
{
XML
<div>
<input type="text" id="EmpName" + i/>
<input type="text" id="Designation" + i />
</div>

}

please tell me what should i place in my Html code and what should my code be in Javascript file.

Any suggestions are appreciated.
Posted

XML
I hope you are new to MVC.

If you have a view model in the cshtml file, just use the view model and loop the same way to generate the controls dynamically.

Ex:

If your view model is Demo.ViewModels.EmployeeData

foreach (Demo.ViewModels.EmployeeData emp in  Model.EmployeeViewList)
{

<div>
<@Html.TextBoxFor(model => emp.EmpName)
<@Html.TextBoxFor(model => emp.Designation)
</div>

}


You can also use for loop instead of foreach loop
 
Share this answer
 
for(int i=0; i<4; i++)
{
    <div>
        @Html.TextBox("emp" + i);
        @Html.TextBox("desg" + i);
    </div>
    <hr />
}

Should work ;)

-KR
 
Share this answer
 

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