1.在ASP.Net MVC开发WEB中经常下拉列表,其实我们可以配置到后台调用可以自定义@Html 控件来使用
使用 MvcHtmlString 来构造后来下拉相关控件 (这里我是 使用 Select2 来创建下拉)
using 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.前端调用
<tr>
<td>
下拉插件:
</td>
<td colspan="3">
@TEST.Areas.Common.Extend.HtmlExtend.HtmlControl.Select2BusinessArea("相关参数")
</td>
</tr>