MVC的二级联动

博主今日使用Address表进行二级联动操作,包括添加值和视图页面相关操作,聚焦于信息技术中数据库表的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   今天用一张表做下二级联动,现在有一张表Address

create table Address
(
Id int primary key identity,--主键
AName varchar(50),--地名
PId int
)

添加值

insert into Address values('上海',0)
insert into Address values('北京',0)
insert into Address values('奉贤',1)
insert into Address values('浦东',1)
insert into Address values('静安',1)
insert into Address values('朝阳',2)
insert into Address values('房山',2)
insert into Address values('海淀',2)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using 二级联动.EF;
using Newtonsoft.Json;
namespace 二级联动.Controllers
{
    public class StudentController : Controller
    {
        A v = new A();
        // GET: Student
        public ActionResult GetShi()
        {
            Model1 n = new Model1();
        
            List<Address> list =v.GetAresses(0) ;
            var sel = from a in list
                      select new SelectListItem
                      {
                          Text = a.AName,
                          Value = a.AId.ToString()
                      };
            ViewBag.a = sel;
            //GetQu(1);
            return View();
        }
        public ActionResult GetQu(int pid)
        {
            List<Address> list = v.GetAresses(pid);
            var sel = from a in list
                      select new SelectListItem
                      {
                          Text = a.AName,
                          Value = a.AId.ToString()
                      };
 


            return Json(sel);


        }

    }
    class A
    {
        public List<Address> GetAresses(int pid)
        {
            Model1 m = new Model1();
          return  m.Address.Where(s => s.PId == pid).ToList();

        }
    }
   
 
 
}

 

 

视图页面


@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <script src="~/Scripts/jquery-1.10.2.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <meta name="viewport" content="width=device-width" />
    <title>GetShi</title>
</head>
<body>
    <div>
        <table>
            <tr>
                <th>
                    市:
                </th>
                <th>
                    @Html.DropDownList("sid", ViewBag.a as IEnumerable<SelectListItem>)
                </th>
            </tr>
            <tr>
                <th>
                    区:
                </th>
                <th>
                    <select id="qid"></select>
                </th>
            </tr>
        </table>
    </div>
</body>
</html>
<script>
    $("#sid").change(function () {

        $.ajax({

            url: '/Student/GetQu',
            type: 'post',
            data: { 'pid': $("#sid").val() },
            success: function (data) {
                $("#qid").empty();
                alert(312);
                $(data).each(function () {
                    var option = " <option value=" + this.Value + ">" + this.Text + "</option>";
                    $('#qid').append(option);

                })
                
            }
        })

    })

</script>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值