MVC前台界面调用方式如下:

 @Html.AreaDropDownList("areaCode", areaCode, , string.Empty)

参数说明:

第一个参数控件的名称;
 第二个参数选中的地区编码;
 第三个参数地区层级;
 第四个参数根级地区;

地区数据库表设计如下:

MVC地区多级联动扩展实现(非递归形式)-LMLPHP

下拉列表的扩展:

   #region 下拉列表

         /// <summary>
/// 联动下拉列表
/// </summary>
/// <param name="htmlHelper">被扩展的HtmlHelper实例</param>
/// <param name="expression">获取数据集合</param>
/// <param name="level">显示多少级</param>
/// <param name="defaultValue">TProperty类型的默认值(如string默认值为"")</param>
/// <param name="rootItems">获取根级列表数据</param>
/// <param name="getParentID">获取列表项的ParentID方法</param>
/// <param name="getChildItems">获取子级列表数据集合方法</param>
/// <param name="getChildSelectDataUrl">获取子级列表数据的远程地址</param>
/// <returns>html代码</returns>
public static MvcHtmlString LinkageDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, TProperty defaultValue, int level, Dictionary<TProperty, string> rootItems,Func<TProperty, TProperty> getParentID, Func<TProperty, Dictionary<TProperty, string>> getChildItems,string getChildSelectDataUrl,string optionLabel = "请选择")
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); return LinkageDropDownList(htmlHelper, ExpressionHelper.GetExpressionText(expression), (TProperty)metadata.Model, defaultValue, level, rootItems, getParentID,getChildItems,getChildSelectDataUrl,optionLabel);
} /// <summary>
/// 联动下拉列表
/// </summary>
/// <param name="htmlHelper">被扩展的HtmlHelper实例</param>
/// <param name="level">显示多少级</param>
/// <param name="defaultValue">TProperty类型的默认值(如string默认值为"")</param>
/// <param name="name">下拉列表表单项名</param>
/// <param name="selectedValue">当前选中值</param>
/// <param name="rootItems">获取根级列表数据</param>
/// <param name="getParentId">获取列表项的ParentID方法</param>
/// <param name="getChildItems">获取子级列表数据集合方法</param>
/// <param name="getChildSelectDataUrl">获取子级列表数据的远程地址</param>
/// <returns>html代码</returns>
public static MvcHtmlString LinkageDropDownList<TProperty>(this HtmlHelper htmlHelper, string name, TProperty selectedValue,TProperty defaultValue, int level, Dictionary<TProperty, string> rootItems, Func<TProperty, TProperty> getParentId, Func<TProperty, Dictionary<TProperty, string>> getChildItems, string getChildSelectDataUrl, string optionLabel = "请选择")
{
string fullName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
//select data init
Stack<Dictionary<TProperty, string>> stack = new Stack<Dictionary<TProperty, string>>(); //如果有选中的值,则查找其所在列表前面的所有列表
IList<TProperty> selectedValues = new List<TProperty>();
if (selectedValue != null && !selectedValue.Equals(defaultValue))
{
TProperty itemId = selectedValue;
TProperty parentItemId = getParentId(itemId);
while (!itemId.Equals(defaultValue) && !parentItemId.Equals(defaultValue))
{
stack.Push(getChildItems(parentItemId));
selectedValues.Add(itemId);
itemId = parentItemId;
parentItemId = getParentId(itemId);
}
if (rootItems.Count() > )
{
TProperty rootId = getParentId(rootItems.First().Key);
if (!itemId.Equals(rootId))
{
stack.Push(rootItems);
selectedValues.Add(itemId);
}
}
}
else
{
TProperty rootItemID = rootItems.Select(n => n.Key).FirstOrDefault();
stack.Push(rootItems);
} //生成标签
TagBuilder containerBuilder = new TagBuilder("span");
containerBuilder.MergeAttribute("plugin", "linkageDropDownList");
var data = new Dictionary<string, object>();
data.TryAdd("GetChildSelectDataUrl", getChildSelectDataUrl);
data.TryAdd("ControlName", name);
data.TryAdd("Level", level);
data.TryAdd("OptionLabel", optionLabel);
data.TryAdd("DefaultValue", defaultValue.ToString());
containerBuilder.MergeAttribute("data", Json.Encode(data));
int currentIndex = ;
while (stack.Count > )
{
Dictionary<TProperty, string> dictionary = stack.Pop();
IEnumerable<SelectListItem> selectList = dictionary.Select(n => new SelectListItem() { Selected = selectedValues.Contains(n.Key), Text = n.Value, Value = n.Key.ToString() });
containerBuilder.InnerHtml += "\r\n" + htmlHelper.DropDownList(string.Format("{0}_{1}", name, currentIndex), selectList,
optionLabel, new { @class = "tn-dropdownlist" });
currentIndex++;
}
containerBuilder.InnerHtml += "\r\n" + htmlHelper.Hidden(name);
return MvcHtmlString.Create(containerBuilder.ToString());
} #endregion

对外的调用的实现:

         /// <summary>
/// 地区下拉列表
/// </summary>
/// <param name="htmlHelper">被扩展的htmlHelper实例</param>
/// <param name="expression">选择实体中类别属性的lamda表达式</param>
/// <param name="areaLevel">地区层级(默认取站点地区配置)</param>
/// <param name="rootAreaCode">根级地区(默认取站点地区配置)</param>
public static MvcHtmlString AreaDropDownListFor<TModel>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, string>> expression, int? areaLevel = null, string rootAreaCode = null)
{
string getChildAreasUrl = "/Channel/GetChildAreas";//获取子地区的远程地址,(AreaCode,name)的JSON数据
if (areaLevel == null)
{
areaLevel = ;//区域设置级别,可以作为配置文件获取
}
return htmlHelper.LinkageDropDownListFor<TModel, string>(expression, string.Empty, areaLevel.Value, 获取根级区域的(AreaCode,name)键值对,获取当前选中区域的父区域, 获取当前的子区域, getChildAreasUrl);
} /// <summary>
/// 地区下拉列表
/// </summary>
/// <param name="htmlHelper">被扩展的htmlHelper实例</param>
/// <param name="name">控件name属性</param>
/// <param name="name">选中的地区编码</param>
/// <param name="areaLevel">地区层级(默认取站点配置)</param>
/// <param name="rootAreaCode">根级地区(默认取站点地区配置)</param>
public static MvcHtmlString AreaDropDownList(this HtmlHelper htmlHelper, string name, string value, int? areaLevel = null, string rootAreaCode = null)
{
string getChildAreasUrl = "/Channel/GetChildAreas";//获取子地区的远程地址,(AreaCode,name)的JSON数据
if (areaLevel == null)
{
areaLevel = ;//区域设置级别,可以作为配置文件获取
}
return htmlHelper.LinkageDropDownList<string>(name, value, string.Empty, areaLevel.Value, 获取根级区域的(AreaCode,name)键值对, 获取当前选中区域的父区域, 获取选中地区当前的子区域, getChildAreasUrl);
}
05-11 13:49