前段时间,在项目中遇到了一个问题,在会员中心当会员注册或者会员修改自己地区信息的时候,如果选择最底级地区,整个地区回退到最顶级地区的选择菜单,整个详细的地区信息不能正常显示。于是,从前端js函数代码着手调查产生这个效果的原因。刚开始的时候,在前端js函数中增加判断所获取的地区是否是最底级地区的判断,,但是一直解决不了这个问题。后来,转变思路,在后台获取地区信息的函数中当用户选择是底级地区(不存在子级地区)的时候,将它对应的所有父级地区信息全部保存起来。这样当选择底级地区的时候,锁获取的地区信息就不为空。
所以特意来记录一下。
前台调用后台代码:
1: <span id="RegionSpan"></span>
2: <script type="text/javascript">
3: window.onload = function () {
4: getCascadeNoStyle(<%=Model == null ? 0 : Model.RegionId%>, "RegionId", "RegionId", "/Region/AjaxRegion", "RegionSpan");
5: }
6:
7: </script>
8:
1: // 地区联动菜单
2: // id 当前id
3: // idName 列名
4: // key 后台接收属性名
5: // url 请求分类地址
6: // spanId放的位置
7: function getCascadeNoStyle(id, idName, key, url, spanId) {
8:
9: $.getJSON(url + "/" + id, { rnum: Math.random() }, function (result) {
10:
11: var html = "";
12: var str = ',"' + idName + '","' + key + '","' + url + '","' + spanId + '"';
13: if (result.length < 1) {
14: html += "<select hncetype='area' id='lbArea' name='lbArea' onchange='getCascadeNoStyle(this.options[this.selectedIndex].value" + str + ")'>";
15: html += '<option value="0" >请选择</option>';
16: html += '</select>';
17: }
18:
19: for (var i = result.length - 1; i > -1; i--) {
20: html += "<select hncetype='area' id='lbArea' name='lbArea' onchange='getCascadeNoStyle(this.options[this.selectedIndex].value" + str + ")'>";
21: html += '<option value="0" >请选择</option>';
22: for (var j = 0; j < result[i].Lists.length; j++) {
23: if (eval("result[" + i + "].Lists[" + j + "]" + "." + idName) == result[i].SelectId) {
24: html += '<option value="' + eval("result[" + i + "].Lists[" + j + "]" + "." + idName) + '" selected>' + result[i].Lists[j].Name + '</option>';
25: }
26: else {
27: html += '<option value="' + eval("result[" + i + "].Lists[" + j + "]" + "." + idName) + '">' + result[i].Lists[j].Name + '</option>';
28: }
29: }
30: html += '</select>';
31: }
32: html += "<input type='hidden' value='" + id + "' name='" + key + "' id='" + key + "'/>";
33: document.getElementById(spanId).innerHTML = html;
34: });
35: }
36:
后台RgionController.cs中AjaxRegion() 的定义如下所示:
1: public ActionResult AjaxRegion(int id)
2: {
3: List<MultipleViewData<RegionDTO>> viewDatas = new List<MultipleViewData<RegionDTO>>();
4:
5: int level = 1;
6: level = RegionBiz.Instance.GetLevelByRegionId(id, ref level);
7: int nowId = id;
8: int selectId = 0;
9: int regionsCount = RegionBiz.Instance.GetCountByParentId(nowId);
10: //当选择的地区是底级地区,即不存在子级地区的时候,必须将这个底级地区的所有父级地区获取并且保存
//level=1,表"",level=2,表示省级,level=3,表示市级,level=4,表示县级,level=5,表示乡级
11: if (regionsCount == 0)
12: {
13: if (level == 5)
14: {
15: RegionDTO r1 = RegionBiz.Instance.GetById(nowId);
16: List<RegionDTO> region = RegionBiz.Instance.GetRegionByParentId(r1.ParentId);
17: selectId = r1.RegionId;
18: MultipleViewData<RegionDTO> review1 = new MultipleViewData<RegionDTO>() { Lists = region, Level = level - 1, SelectId = selectId };
19: viewDatas.Add(review1);
20:
21:
22: RegionDTO r2 = RegionBiz.Instance.GetById(r1.ParentId);
23: List<RegionDTO> region2 = RegionBiz.Instance.GetRegionByParentId(r2.ParentId);
24: selectId = r2.RegionId;
25: MultipleViewData<RegionDTO> review2 = new MultipleViewData<RegionDTO>() { Lists = region2, Level = level - 2, SelectId = selectId };
26: viewDatas.Add(review2);
27:
28:
29: RegionDTO r3 = RegionBiz.Instance.GetById(r2.ParentId);
30: List<RegionDTO> region3 = RegionBiz.Instance.GetRegionByParentId(r3.ParentId);
31: selectId = r3.RegionId;
32: MultipleViewData<RegionDTO> review3 = new MultipleViewData<RegionDTO>() { Lists = region3, Level = level - 3, SelectId = selectId };
33: viewDatas.Add(review3);
34:
35:
36:
37: RegionDTO r4 = RegionBiz.Instance.GetById(r3.ParentId);
38: List<RegionDTO> region4 = RegionBiz.Instance.GetRegionByParentId(r4.ParentId);
39: selectId = r4.RegionId;
40: MultipleViewData<RegionDTO> review4 = new MultipleViewData<RegionDTO>() { Lists = region4, Level = level - 4, SelectId = selectId };
41: viewDatas.Add(review4);
42: }
43: else if (level == 4)
44: {
45: RegionDTO r1 = RegionBiz.Instance.GetById(nowId);
46: List<RegionDTO> region = RegionBiz.Instance.GetRegionByParentId(r1.ParentId);
47: selectId = r1.RegionId;
48: MultipleViewData<RegionDTO> review1 = new MultipleViewData<RegionDTO>() { Lists = region, Level = level - 1, SelectId = selectId };
49: viewDatas.Add(review1);
50:
51: RegionDTO r2 = RegionBiz.Instance.GetById(r1.ParentId);
52: List<RegionDTO> region2 = RegionBiz.Instance.GetRegionByParentId(r2.ParentId);
53: selectId = r2.RegionId;
54: MultipleViewData<RegionDTO> review2 = new MultipleViewData<RegionDTO>() { Lists = region2, Level = level - 2, SelectId = selectId };
55: viewDatas.Add(review2);
56:
57:
58: RegionDTO r3 = RegionBiz.Instance.GetById(r2.ParentId);
59: List<RegionDTO> region3 = RegionBiz.Instance.GetRegionByParentId(r3.ParentId);
60: selectId = r3.RegionId;
61: MultipleViewData<RegionDTO> review3 = new MultipleViewData<RegionDTO>() { Lists = region3, Level = level - 3, SelectId = selectId };
62: viewDatas.Add(review3);
63: }
64: else if (level == 3)
65: {
66: RegionDTO r1 = RegionBiz.Instance.GetById(nowId);
67: List<RegionDTO> region = RegionBiz.Instance.GetRegionByParentId(r1.ParentId);
68: selectId = r1.RegionId;
69: MultipleViewData<RegionDTO> review1 = new MultipleViewData<RegionDTO>() { Lists = region, Level = level - 1, SelectId = selectId };
70: viewDatas.Add(review1);
71:
72: RegionDTO r2 = RegionBiz.Instance.GetById(r1.ParentId);
73: List<RegionDTO> region2 = RegionBiz.Instance.GetRegionByParentId(r2.ParentId);
74: selectId = r2.RegionId;
75: MultipleViewData<RegionDTO> review2 = new MultipleViewData<RegionDTO>() { Lists = region2, Level = level - 2, SelectId = selectId };
76: viewDatas.Add(review2);
77: }
78: else if (level == 2)
79: {
80: RegionDTO r1 = RegionBiz.Instance.GetById(nowId);
81: List<RegionDTO> region = RegionBiz.Instance.GetRegionByParentId(r1.ParentId);
82: selectId = r1.RegionId;
83: MultipleViewData<RegionDTO> review1 = new MultipleViewData<RegionDTO>() { Lists = region, Level = level - 1, SelectId = selectId };
84: viewDatas.Add(review1);
85: }
86: }
87:
88: for (int i = 0; i < level; i++)
89: {
90: List<RegionDTO> region = RegionBiz.Instance.GetRegionByParentId(nowId);
91: if (region.Count == 0)
92: {
93: RegionDTO r = RegionBiz.Instance.GetById(nowId);
94: region = RegionBiz.Instance.GetRegionByParentId(r.ParentId);
95: selectId = r.RegionId;
96: }
97: else if (region.Count > 0)
98: {
99: RegionDTO UpRegion = RegionBiz.Instance.GetById(nowId);
100: MultipleViewData<RegionDTO> review = new MultipleViewData<RegionDTO>() { Lists = region, Level = i, SelectId = selectId };
101: viewDatas.Add(review);
102: if (UpRegion != null)
103: {
104: nowId = UpRegion.ParentId;
105: selectId = UpRegion.RegionId;
106: }
107: }
108: }
109: //if ((regionsCount == 0) && (viewDatas.Count>0)) { viewDatas.Remove(viewDatas[0]); }
110: return Json(viewDatas, JsonRequestBehavior.AllowGet);
111: }
112:
113:
114:
115:
运行效果如下所示: