搜索

查看: 3081|回复: 11

[ASP.NET] ASP.NET MVC实现区域或城市选择

[复制链接]
发表于 2023-5-4 11:33:25 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:33:25 3081 11 看全部
每次在"万达影城"网上购票总会用到左上角选择城市的功能。如下:

202283183418421.png

202283183418421.png


今天就在ASP.NET MVC中实现一下。我想最好的方式应该是写一个插件,但自己在这方面的功力尚欠缺,如果大家在这方面有好的解决方案,希望在这一起交流,那将会更好。
大致思路如下:
  • 点击"更换"弹出div,用bootstrap来实现
  • div中的tabs,用jqueryui来实现
  • tab项中的城市,用jquery.tmpl.min.js模版来实现
    有关城市的Model:
        public class City
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string FirstLetter { get; set; }
        }
    在Shared文件夹下的_Layout.cshtml中,引用jquery, jqueryui, bootstrap相关的css和js文件。

       
       
        @ViewBag.Title
        @Styles.Render("~/Content/css")
       
       
        @RenderSection("styles", required: false)
        @Scripts.Render("~/bundles/jquery")
       
       

        @RenderBody()
       
        @RenderSection("scripts", required: false)

    在Home/Index.cshtml视图中:
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    @section styles
    {
       
    }
       
            
                Toggle navigation
                
                
                
            

       
       
            
                
                    全国[更换]
                   
                        
                            
  • ABCD
                            
  • EFGH
                            
  • IJKL
                            
  • MNOP
                            
  • QRST
                            
  • UVWX
                            
  •   YZ
                        

                        
                            
                        
                        
                            
                        
                        
                            
                            
                        
                        
                            
                            
                        
                        
                            
                            
                        
                        
                            
                            
                        
                        
                            
                            
                        
                   
                
            

       
       
    @section scripts
    {
       
       
       
       
    }
    以上,
    bootstrap显示导航菜单,点击"更换",弹出一个id为cities的div,其中包含jqueryui的tab。然后异步加载json数据到id为cityTemplate的模版上,最后追加到对应的区域。
    在HomeController中:
    using System.Linq;
    using System.Web.Mvc;
    namespace MvcApplication1.Controllers
    {
        public class HomeController : Controller
        {
            //
            // GET: /Home/
            public ActionResult Index()
            {
                return View();
            }
            //获取首字母是ABCD的城市
            public ActionResult GetCitiesByABCD()
            {
                var cities =
                    Database.GetCities()
                        .Where(
                            c =>
                                c.FirstLetter == "A" || c.FirstLetter == "B" || c.FirstLetter == "C" || c.FirstLetter == "D");
                var result = from c in cities
                    select new {city = c.Name};
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            //获取首字母是EFGH的城市
            public ActionResult GetCitiesByEFGH()
            {
                var cities =
                    Database.GetCities()
                        .Where(
                            c =>
                                c.FirstLetter == "E" || c.FirstLetter == "F" || c.FirstLetter == "G" || c.FirstLetter == "H");
                var result = from c in cities
                             select new { city = c.Name };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            //获取首字母是IJKL的城市
            public ActionResult GetCitiesByIJKL()
            {
                var cities =
                    Database.GetCities()
                        .Where(
                            c =>
                                c.FirstLetter == "I" || c.FirstLetter == "J" || c.FirstLetter == "K" || c.FirstLetter == "L");
                var result = from c in cities
                             select new { city = c.Name };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            //获取首字母是MNOP的城市
            public ActionResult GetCitiesByMNOP()
            {
                var cities =
                    Database.GetCities()
                        .Where(
                            c =>
                                c.FirstLetter == "M" || c.FirstLetter == "N" || c.FirstLetter == "O" || c.FirstLetter == "P");
                var result = from c in cities
                             select new { city = c.Name };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            //获取首字母是QRST的城市
            public ActionResult GetCitiesByQRST()
            {
                var cities =
                    Database.GetCities()
                        .Where(
                            c =>
                                c.FirstLetter == "Q" || c.FirstLetter == "R" || c.FirstLetter == "S" || c.FirstLetter == "T");
                var result = from c in cities
                             select new { city = c.Name };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            //获取首字母是UVWX的城市
            public ActionResult GetCitiesByUVWX()
            {
                var cities =
                    Database.GetCities()
                        .Where(
                            c =>
                                c.FirstLetter == "U" || c.FirstLetter == "V" || c.FirstLetter == "W" || c.FirstLetter == "X");
                var result = from c in cities
                             select new { city = c.Name };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            //获取首字母是YZ的城市
            public ActionResult GetCitiesByYZ()
            {
                var cities =
                    Database.GetCities()
                        .Where(
                            c =>
                                c.FirstLetter == "Y" || c.FirstLetter == "Z");
                var result = from c in cities
                             select new { city = c.Name };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }
    }
    最后呈现的效果:

    202283183418422.png

    202283183418422.png


    有关CitySelect.css文件:
    .dropdown-large {
      position: static !important;
    }
    .dropdown-menu-large {
      margin-left: 16px;
      margin-right: 16px;
      padding: 20px 0px;
    }
    .dropdown-menu-large > li > ul {
      padding: 0;
      margin: 0;
    }
    .dropdown-menu-large > li > ul > li {
      list-style: none;
    }
    .dropdown-menu-large > li > ul > li > a {
      display: block;
      padding: 3px 20px;
      clear: both;
      font-weight: normal;
      line-height: 1.428571429;
      color: #333333;
      white-space: normal;
    }
    .dropdown-menu-large > li ul > li > a:hover,
    .dropdown-menu-large > li ul > li > a:focus {
      text-decoration: none;
      color: #262626;
      background-color: #f5f5f5;
    }
    .dropdown-menu-large .disabled > a,
    .dropdown-menu-large .disabled > a:hover,
    .dropdown-menu-large .disabled > a:focus {
      color: #999999;
    }
    .dropdown-menu-large .disabled > a:hover,
    .dropdown-menu-large .disabled > a:focus {
      text-decoration: none;
      background-color: transparent;
      background-image: none;
      filter: progid:DXImageTransform.Microsoft.gradient(enabled = false);
      cursor: not-allowed;
    }
    .dropdown-menu-large .dropdown-header {
      color: #428bca;
      font-size: 18px;
    }
    @media (max-width: 768px) {
      .dropdown-menu-large {
        margin-left: 0 ;
        margin-right: 0 ;
      }
      .dropdown-menu-large > li {
        margin-bottom: 30px;
      }
      .dropdown-menu-large > li:last-child {
        margin-bottom: 0;
      }
      .dropdown-menu-large .dropdown-header {
        padding: 3px 15px !important;
      }
    }
    #cities {
        width: 620px;
        padding: 10px;
    }
    #cities ul {
        width: 600px;
    }
    #cities div {
        width: 600px;
    }
    #cities li{
        text-align: center;
        width: 80px;
        height: 30px;
        padding: 5px;
    }
    .rc {
        margin-right: 20px;
    }
    有关Database类:
    using System.Collections.Generic;
    using MvcApplication1.Models;
    namespace MvcApplication1
    {
        public class Database
        {
            public static IEnumerable GetCities()
            {
                return new List()
                {
                    new City(){Id = 1, Name = "包头", FirstLetter = "B"},
                    new City(){Id = 2, Name = "北京", FirstLetter = "B"},
                    new City(){Id = 3, Name = "长春", FirstLetter = "C"},
                    new City(){Id = 4, Name = "大同", FirstLetter = "D"},
                    new City(){Id = 5, Name = "福州", FirstLetter = "F"},
                    new City(){Id = 6, Name = "广州", FirstLetter = "G"},
                    new City(){Id = 7, Name = "哈尔滨", FirstLetter = "H"},
                    new City(){Id = 8, Name = "济南", FirstLetter = "J"},
                    new City(){Id = 9, Name = "昆明", FirstLetter = "K"},
                    new City(){Id = 10, Name = "洛阳", FirstLetter = "L"},
                    new City(){Id = 11, Name = "马鞍山", FirstLetter = "M"},
                    new City(){Id = 12, Name = "南京", FirstLetter = "N"},
                    new City(){Id = 13, Name = "青岛", FirstLetter = "Q"},
                    new City(){Id = 14, Name = "深圳", FirstLetter = "S"},
                    new City(){Id = 15, Name = "天津", FirstLetter = "T"},
                    new City(){Id = 16, Name = "威海", FirstLetter = "W"},
                    new City(){Id = 17, Name = "西安", FirstLetter = "X"},
                    new City(){Id = 18, Name = "烟台", FirstLetter = "Y"},
                    new City(){Id = 19, Name = "郑江", FirstLetter = "Z"}
                };
            }
        }
    }
    以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
  • 回复

    使用道具 举报

    发表于 2023-6-28 21:42:24 | 显示全部楼层
    老橡树1 2023-6-28 21:42:24 看全部
    这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-29 14:28:31 | 显示全部楼层
    知足常乐77 2023-6-29 14:28:31 看全部
    感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
    回复

    使用道具 举报

    发表于 2023-6-30 02:50:18 | 显示全部楼层
    执着等待等wc 2023-6-30 02:50:18 看全部
    论坛不能没有像楼主这样的人才啊!我会一直支持知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-30 03:20:53 | 显示全部楼层
    啤酒瓶空了缓 2023-6-30 03:20:53 看全部
    楼主,大恩不言谢了!知鸟论坛是最棒的!
    回复

    使用道具 举报

    发表于 2023-6-30 14:43:08 | 显示全部楼层
    我的苦恼冉 2023-6-30 14:43:08 看全部
    这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    发表于 2023-6-30 16:23:51 | 显示全部楼层
    术数古籍专卖疤 2023-6-30 16:23:51 看全部
    楼主,大恩不言谢了!知鸟论坛是最棒的!
    回复

    使用道具 举报

    发表于 2023-6-30 16:24:45 | 显示全部楼层
    贰十岁装成熟装s 2023-6-30 16:24:45 看全部
    其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
    回复

    使用道具 举报

    发表于 2023-7-1 00:40:17 | 显示全部楼层
    六翼天使494 2023-7-1 00:40:17 看全部
    感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
    回复

    使用道具 举报

    发表于 2023-7-3 17:20:36 | 显示全部楼层
    123456868 2023-7-3 17:20:36 看全部
    这个帖子不回对不起自己!我想我是一天也不能离开知鸟论坛
    回复

    使用道具 举报

    • 您可能感兴趣
    点击右侧快捷回复 【请勿灌水】
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则 返回列表

    RSS订阅| SiteMap| 小黑屋| 知鸟论坛
    联系邮箱E-mail:zniao@foxmail.com
    快速回复 返回顶部 返回列表