MVC 4如何创建动态控件 [英] MVC 4 how to create dynamic controls

查看:113
本文介绍了MVC 4如何创建动态控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用c#MVC 4,我想创建多个用户控件并动态填充数据。



例如:我有一个历史填写表单并且在其中我必须显示多个问题,有选项控制,可以是单选按钮或复选框也有一个文本区域与每个问题。所以我不知道字段名称及其控制类型访问此页面之前,它获取数据和来自sql表的控件类型有问题:



字段1:文本标签



字段2: DropdownList(值:1,2,3,4,5)或单选按钮或复选框或文本框



字段3:textarea



如何实现这一目标?

I am using c# MVC 4 and I would like to create Multiple user controls and fill data dynamically.

Eg: I have a history filling form and in it i have to display multiple question having options control which can be radio button or check box also have a text-area with every question.So I do not know the field names and its control types prior to visiting this page, it gets the data and control types from a sql table with question:

field 1: Text-Label

field 2: DropdownList (values: 1,2,3,4,5) or radio button or check-box or text-box

field 3: textarea

How do I achieve this?

推荐答案

亲爱的,



您可以使用模板来实现此目的。您可以在共享文件夹下创建显示或编辑器模板。可以基于来自SQL的控件名称来命名模板。因此对于例如你有三个来自SQL的控件名 - TextArea,RadioButton和CheckBox。因此,当您调用Displayfor或Editorfor辅助方法时,它会分别尝试分别在文件夹Views / Shared / DisplayTemplates或Views / Shared / EditorTemplates下查看文件(TextArea.cshtml等)。如果您在实施过程中遇到任何问题,请告诉我。
Hi Honey,

You can use templates to achieve this. You can create display or editor templates under shared folder. Naming of the template can be done based on the control name coming from SQL. So for e.g. u have three control names coming from SQL - TextArea, RadioButton and CheckBox. So when you call Displayfor or Editorfor helper methods it wud try to see files (TextArea.cshtml etc.) under folders Views/Shared/DisplayTemplates or Views/Shared/EditorTemplates respectively respectively. Please let me know if you face any issue while implementing it.


亲爱的,



我知道已经有几个月了你发布了你的问题,你可能已经找到了解决方案。我只是想发布一些链接给你一个想法和其他人。





这里是链接:



http://tutsblog.net/adding-textboxes-dynamically-and-saving-the-values-into-database-in-asp-net-mvc-part-1/ [ ^ ]



http://tutsblog.net/adding-textboxes-dynamically-and-saving-the-values-into-database- in-asp-net-mvc-part-2 / [ ^ ]





让我知道如果这些可以帮到你或者有什么我可以帮助你的话。





干杯!
Hi Honey,

I know it's already been months since you posted your question and you might have found the solution already. I just thought I post some links to give you an idea and to others as well.


here are the link:

http://tutsblog.net/adding-textboxes-dynamically-and-saving-the-values-into-database-in-asp-net-mvc-part-1/[^]

http://tutsblog.net/adding-textboxes-dynamically-and-saving-the-values-into-database-in-asp-net-mvc-part-2/[^]


let me know if these helps you or if there's anything I can help you with.


Cheers!


这篇关于MVC 4如何创建动态控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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