MVC3 메소드(JQuery)를 기반으로 드롭다운 목록 연결 구현_jquery
지난 프로젝트에서 여러 드롭다운 목록을 연결해야 하는 작업을 접했습니다. 오늘은 나중에 참조할 수 있도록 구현 방법을 정리하는 시간을 가졌습니다.
얻을 수 있는 효과는 하나의 드롭다운 상자를 클릭하면 다른 드롭다운 상자의 값도 이에 따라 변경된다는 것입니다. 예를 들어 중국을 선택하면 중국의 각 지역이 다른 드롭다운 상자에 표시됩니다.
기존 HTML 방식은 비교적 간단하고, 사실 MVC 기반의 구현 방식도 유사합니다.
코드 직접 추가:
코드 복사 코드는 다음과 같습니다.
공용 클래스 DP_Provice
{
공용 int proviceID { get; set; }
공용 문자열 ProviceName { get; set }
public int ProviceNode } virtual List
}
public int CityNode }
public string { get; set; }
public string ProviceNode { get; set; }
}
위에 관련된 엔터티 클래스에 값을 할당하고 임시로 정적 클래스를 사용하여 간단한 데이터를 초기화합니다.
코드 복사 코드는 다음과 같습니다. public static class DPDataSource
{ 공용 정적 목록 InitalData()
{
목록 소스 = 새 목록
{
new DP_Provice{ ProviceNode=1, ProviceName="Beijing", citySource =new List{
new DP_City{
CityNode=11, CityName="Beijing Haidian"
},
new DP_City{
CityNode=12,CityName="Beijing Xicheng"
}
}},
new DP_Provice{ ProviceNode=2, ProviceName="Shandong", citySource=new List{
new DP_City{
CityNode=21, CityName=" 지난"
},
새 DP_City{
CityNode=22,CityName="Dezhou"
}
}},
새 DP_Provice{ ProviceNode=3, ProviceName="허베이" , citySource=new List
>new DP_City{
CityNode=31, CityName="Shijiazhuang"
},
new DP_City{
CityNode=32,CityName="Hengshui "
}
}}
};
return source;
}
}
Controller의 특정 호출은 AJAX 메서드를 사용합니다. JQuery에서는 결과 유형이 반환됩니다.
{
목록
{
new DP_Provice{ ProviceNode=1, ProviceName="Beijing", citySource =new List
new DP_City{
CityNode=11, CityName="Beijing Haidian"
},
new DP_City{
CityNode=12,CityName="Beijing Xicheng"
}
}},
new DP_Provice{ ProviceNode=2, ProviceName="Shandong", citySource=new List
new DP_City{
CityNode=21, CityName=" 지난"
},
새 DP_City{
CityNode=22,CityName="Dezhou"
}
}},
새 DP_Provice{ ProviceNode=3, ProviceName="허베이" , citySource=new List
>new DP_City{
CityNode=31, CityName="Shijiazhuang"
},
new DP_City{
CityNode=32,CityName="Hengshui "
}
}}
};
return source;
}
}
Controller의 특정 호출은 AJAX 메서드를 사용합니다. JQuery에서는 결과 유형이 반환됩니다.
코드 복사 코드는 다음과 같습니다. 공개 ActionResult Index()
{ return View("DPShow")
}
List source = DPDataSource.InitalData()
public JsonResult GetProvinceSource ()
{
if ( source == null || source.Count < 0)
{
source = DPDataSource.InitalData()
}
return Json(source , JsonRequestBehavior.AllowGet)
}
public JsonResult GetCitySource(string proviceName)
{
source = DPDataSource.InitalData()
List citySource = new List( );
citySource = source.Where( a => a.ProviceNode.ToString().Contains(proviceName)).First().citySource
return Json(citySource, JsonRequestBehavior.AllowGet); >}
이때 데이터를 바인딩하여 Razor 뷰에 표시해야 합니다.
}
List
public JsonResult GetProvinceSource ()
{
if ( source == null || source.Count < 0)
{
source = DPDataSource.InitalData()
}
return Json(source , JsonRequestBehavior.AllowGet)
}
public JsonResult GetCitySource(string proviceName)
{
source = DPDataSource.InitalData()
List
citySource = source.Where( a => a.ProviceNode.ToString().Contains(proviceName)).First().citySource
return Json(citySource, JsonRequestBehavior.AllowGet); >}
이때 데이터를 바인딩하여 Razor 뷰에 표시해야 합니다.
코드 복사
코드는 다음과 같습니다.
@model MvcApplication.Models.DP_Provice
@{
ViewBag.Title = "DPShow";
Layout = "~/Views/Shared/_Layout.cshtml"
}
@model MvcApplication.Models.DP_Provice
@{
ViewBag.Title = "DPShow";
Layout = "~/Views/Shared/_Layout.cshtml"
}

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
4 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
R.E.P.O. 파일 저장 위치 : 어디에 있고 그것을 보호하는 방법은 무엇입니까?
3 몇 주 전
By DDD

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7313
9


자바 튜토리얼
1625
14


Cakephp 튜토리얼
1348
46


라라벨 튜토리얼
1260
25


PHP 튜토리얼
1207
29

