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>