小数据二级联动-封装成一个二级联动部分视图
程序员文章站
2022-07-02 13:42:20
@using OP.WebUI.Areas.Stock.Models @*约定如下: 调用 SetViewBag_Rooms 方法设置 ViewBag.Rooms *@ 所属库房 楼层 using System.Collections.Generic; using Syst... ......
@using op.webui.areas.stock.models @*约定如下: 调用 setviewbag_rooms 方法设置 viewbag.rooms *@ <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 所属库房</label> <div class="col-sm-9"> <select id="roomid" name="roomid" class="width-40 chosen-select form-control" data-placeholder="所属库房"> @foreach (father father in (ienumerable<father>)viewbag.rooms) { if (father.selected) { <option selected="selected" id="option-@father.id" value="@father.id" childs="@father.getoptionsmark()">@father.name</option> } else { <option id="option-@father.id" value="@father.id" childs="@father.getoptionsmark()">@father.name</option> } } </select> </div> </div> <div class="form-group"> <label class="col-sm-3 control-label no-padding-right" for="form-field-1"> 楼层</label> <div class="col-sm-9"> <select id="floorid" name="floorid" class="width-40 chosen-select form-control" data-placeholder="楼层"></select> </div> </div> <script type="text/javascript"> var room = $("#roomid"); room.change(function () { debugger; var roomid = $(this).val(); loadchild(roomid); }); function loadchild(roomid) { if (!roomid) { $("#floorid").html(""); return; } var childs = $("#option-" + roomid).attr("childs"); $("#floorid").html(childs); } loadchild(room.val()); </script>
using system.collections.generic; using system.text; namespace op.webui.areas.stock.models { public sealed class father : item { public ienumerable<child> items { get; set; } /// <summary> /// 获取下拉选项标记 /// </summary> /// <returns></returns> public string getoptionsmark() { stringbuilder sb = new stringbuilder(); foreach (var item in items) { if (item.selected) { sb.append("<option selected=\"selected\" value=\"" + item.id + "\">" + item.name + "</option>"); } else { sb.append("<option value=\"" + item.id + "\">" + item.name + "</option>"); } } return sb.tostring(); } } public sealed class child : item { } public abstract class item { public int id { get; set; } public string name { get; set; } public bool selected { get; set; } } }
//viewbag 调用示例 //rooms 是 父实体集合 floorscatch 是子实体数据源 // on room.id = floor.roomid var roomids = rooms.select(m => m.id); var floorscatch = _floorservice.value.getfloors(roomids); viewbag.rooms = rooms.select(m => new father { id = m.id, name = m.name, selected = m.id == selectedroomid, items = floorscatch.where(n => n.roomid == m.id && n.isenable == true) .select(n => new child { id = n.id, name = n.name, selected = n.id == selectedfloorid }) });
下一篇: socket 函数