基于MVC4中模型值的动态控制生成 [英] Dynamic Control generation base on model value in MVC4

查看:87
本文介绍了基于MVC4中模型值的动态控制生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有一个名为control_id的列,其值为1,2,3,4。基于此值,我必须生成文本框,下拉列表和复选框等控件。 (例如,如果我的control_id为1,则必须生成文本框,并为2,下拉列表等)我对MVC完全不熟悉。任何人都可以指出我正确的方向来实现这种情况吗?

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?

推荐答案

当您将数据返回到View时,您可以在视图上使用razor语法来生成特定控件你想要



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

YourView.cshtml

@model YourViewModel
...
...
...

@if(Model.YourControlId == 1){
   @Html.TextboxFor(x=>x.YourPropertyBinding)
}
else if(Model.YourControlId == 2){
   @Html.DropDownListFor(x=> x.YourPropertyBinding, listOfData)
}
else if(Model.YourControlId == 3){
   .....
}


这篇关于基于MVC4中模型值的动态控制生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆