zoukankan      html  css  js  c++  java
  • 小数据二级联动-封装成一个二级联动部分视图

    @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 })
                });
  • 相关阅读:
    WPF程序内存泄漏问题文章推荐
    UAC影响读写文件和注册表
    .NET COM Interop 中的ReleaseComObject使用准则
    读Excel数据并导入到数据库中最快的方法
    DEP问题文章推荐
    深入了解GC文章推荐
    IIS 发布网站
    iis5.1安装方法(适用于XP),IIS5.0安装包下载、IIS5.1安装包下载、IIS6.0安装包下载
    单击GridView的某一行 根据此行id得到这行的所有信息
    asp.net背景图片自动适应屏幕大小
  • 原文地址:https://www.cnblogs.com/zhuwansu/p/10039776.html
Copyright © 2011-2022 走看看