加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

MVC DropDownList 利用Ajax联动

发布时间:2020-12-16 00:49:43 所属栏目:百科 来源:网络整理
导读:页面代码: [xhtml] view plain copy td style = "text-align:right;width:100px;" 城市/区域: / td td style = "width:170px;text-align:left" %=Html.DropDownListFor( m = m.City,newSelectList(Test.TestPersistence.TestDao.GetCodeDict(11),"Id","Nam

页面代码:

[xhtml] view plain copy
  1. <tdstyle="text-align:right;width:100px;">
  2. 城市/区域:
  3. </td tdstyle="width:170px;text-align:left">
  4. <%=Html.DropDownListFor(m=>m.City,newSelectList(Test.TestPersistence.TestDao.GetCodeDict(11),"Id","Name"))%>m.Region,newSelectList(Test.TestPersistence.TestDao.GetCodeDict(1001),0); background-color:inherit; font-weight:bold">>

javascript代码:ajax的

[javascript] copy
    <mce:scripttype="text/javascript"><!--
  1. $(document).ready(function(){
  2. $("#City").change(function(){
  3. varselec=$("#City").val();
  4. $("#Region").get(0).options.length=0;
  5. $.getJSON("RegionByCity?pid="+selec,{'City':selec},function(data){
  6. for(vari=0;i<data.length;i++){
  7. $("#Region").append("<optionvalue='"+data[i].Id+"'>"+data[i].Name+"</option>");
  8. }
  9. });
  10. });
  11. //--></mce:script>

control的代码:

[c-sharp] copy
    publicActionResultRegionByCity()
  1. {
  2. intpid=Convert.ToInt32(Request.QueryString["pid"]);
  3. CodeDaocodeDao=newCodeDao();
  4. varcodeList=codeDao.GetCodes(pid);
  5. if(Request.IsAjaxRequest())
  6. {
  7. returnJson(codeList,JsonRequestBehavior.AllowGet);
  8. else
  9. returnView("");
  10. }

逻辑层:

copy
    publicIList<Code>GetCodes(intpid)
  1. using(ISessionsession=sessions.OpenSession())
  2. IQueryquery=session.CreateQuery("fromCodecwherec.PId=:pidorderbyId");
  3. query=query.SetInt32("pid",pid);
  4. returnquery.List<Code>();
  5. }
  6. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读