1.在ASP.Net MVC开发WEB中经常下拉列表,其实我们可以配置到后台调用可以自定义@Html 控件来使用
使用 MvcHtmlString 来构造后来下拉相关控件 (这里我是 使用 Select2 来创建下拉)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31using DataFactory; using EntityFactory; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; namespace TEST.Areas.Common.Extend.HtmlExtend { public class HtmlControl { public static MvcHtmlString Select2BusinessArea(this HtmlHelper helper, string dataKey, string verify = "", bool multiple = false) { // Create tag builder StringBuilder html = new StringBuilder(); html.Append("<select class="form-control select2" data-key="" + dataKey + "" data-verify="" + verify + """ + (multiple ? " multiple" : "") + ">"); html.Append("<option value="">请选择...</option>"); for (int i = 0; i < BusinessAreas.Count; i++) { html.Append(string.Format("<option value="{0}">{1}</option>", BusinessAreas[i].BusinessAreaId, BusinessAreas[i].AreaName)); } html.Append("</select>"); // Render tag return MvcHtmlString.Create(html.ToString()); } } }
2.前端调用
复制代码
1
2
3
4
5
6
7
8<tr> <td> 下拉插件: </td> <td colspan="3"> @TEST.Areas.Common.Extend.HtmlExtend.HtmlControl.Select2BusinessArea("相关参数") </td> </tr>
最后
以上就是真实咖啡豆最近收集整理的关于C#在MVC框架中使用后台生成Html模板控件的全部内容,更多相关C#在MVC框架中使用后台生成Html模板控件内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复