在MVC 4.0中动态生成HTML控件 [英] Generate HTML controls dynamically in MVC 4.0
本文介绍了在MVC 4.0中动态生成HTML控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究MVC应用程序。我想根据用户输入动态生成HTML控件。类似
如果userInput = 3(生成三行,这些控件)
for(i = 0; i< 3; i ++)
{
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++)
{
<div>
<input type="text" id="EmpName" + i/>
<input type="text" id="Designation" + i />
</div>
}
请告诉我应该在我的Html代码中放置什么以及我的代码应该在Javascript文件中。
任何建议都表示赞赏。
}
please tell me what should i place in my Html code and what should my code be in Javascript file.
Any suggestions are appreciated.
推荐答案
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>
}
你C一个也用于循环而不是foreach循环
You can also use for loop instead of foreach loop
for(int i=0; i<4; i++)
{
<div>
@Html.TextBox("emp" + i);
@Html.TextBox("desg" + i);
</div>
<hr />
}
应该工作;)
-KR
Should work ;)
-KR
这篇关于在MVC 4.0中动态生成HTML控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文