MVC DropDownList 利用Ajax联动

前端之家收集整理的这篇文章主要介绍了MVC DropDownList 利用Ajax联动前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

页面代码

[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. @H_403_256@if(Request.IsAjaxRequest())
  6. {
  7. @H_403_256@returnJson(codeList,JsonRequestBehavior.AllowGet);
  8. else
  9. returnView("");
  10. }

逻辑层:

copy
    publicIList<Code>GetCodes(intpid) @H_403_256@using(ISessionsession=sessions.OpenSession())
  1. IQueryquery=session.CreateQuery("fromCodecwherec.PId=:pidorderbyId");
  2. query=query.SetInt32("pid",pid);
  3. returnquery.List<Code>();
  4. }
  5. }
原文链接:https://www.f2er.com/ajax/165635.html

猜你在找的Ajax相关文章