页面代码:
- <td style="text-align: right; width: 100px;">
- 城市/区域:
- </td>
- <td style="width: 170px; text-align: left">
- <%= Html.DropDownListFor(m => m.City, new SelectList(Test.TestPersistence.TestDao.GetCodeDict(11), "Id", "Name"))%>
- <%= Html.DropDownListFor(m => m.Region, new SelectList(Test.TestPersistence.TestDao.GetCodeDict(1001), "Id", "Name"))%>
- </td>
javascript代码:ajax的
- <mce:script type="text/javascript"><!--
- $(document).ready(function() {
- $("#City").change(function() {
- var selec = $("#City").val();
- $("#Region").get(0).options.length = 0;
- $.getJSON("RegionByCity?pid=" + selec, { 'City': selec }, function(data) {
- for (var i = 0; i < data.length; i++) {
- $("#Region").append("<option value='" + data[i].Id + "'>" + data[i].Name + "</option>");
- }
- });
- });
- });
- // --></mce:script>
control的代码:
- public ActionResult RegionByCity()
- {
- int pid = Convert.ToInt32(Request.QueryString["pid"]);
- CodeDao codeDao = new CodeDao();
- var codeList = codeDao.GetCodes(pid);
- if (Request.IsAjaxRequest())
- {
- return Json(codeList, JsonRequestBehavior.AllowGet);
- }
- else
- {
- return View("");
- }
- }
逻辑层:
- public IList<Code> GetCodes(int pid)
- {
- using (ISession session = sessions.OpenSession())
- {
- IQuery query = session.CreateQuery("from Code c where c.PId =:pid order by Id ");
- query = query.SetInt32("pid", pid);
- return query.List<Code>();
- }
- }