2013-08-16 49 views
0

上午使用asp.net mvc3,我有2个表格,我希望从下拉列表中获取数据,基于此另一个下拉列表必须执行。例如,如果我选择国家它必须显示状态属于该国家,我正在控制器中使用以下代码。如何在mvc中使用级联下拉列表

ViewBag.country= new SelectList(db.country, "ID", "Name", "--Select--"); 
ViewBag.state= new SelectList("", "stateID", "Name"); 

    @Html.DropDownListFor(model => model.Country, (IEnumerable<SelectListItem>)ViewBag.country, "-Select-") 

    @Html.DropDownListFor(model => model.state, (IEnumerable<SelectListItem>)ViewBag.state, "-Select-") 

但通过使用这我能够得到只有国家。

+0

** [检查此链接](http://stackoverflow.com/a/18169875/2015869)** –

回答

0

有一个很好的jQuery plugin,可以帮助这...

你不想刷新整个页面每次有人更改国家下拉 - 一个AJAX调用简单地下来就是更新状态下降更加用户友好。

0

jquery Ajax是这类问题的最佳选择。

脚本代码下面给出

<script type="text/javascript"> 
    $(function() { 
      $("#@Html.FieldIdFor(model => model.Country)").change(function() { 
       var selectedItem = $(this).val(); 
       var ddlStates = $("#@Html.FieldIdFor(model => model.state)"); 

       $.ajax({ 
        cache:false, 
        type: "GET", 
        url: "@(Url.Action("GetStatesByCountryId", "Country"))", 
        data: "countryId=" , 
        success: function (data) { 
         ddlStates.html(''); 
         $.each(data, function(id, option) { 
          ddlStates.append($('<option></option>').val(option.id).html(option.name));//Append all states to state dropdown through returned result 
         }); 
         statesProgress.hide(); 
        }, 
        error:function (xhr, ajaxOptions, thrownError){ 
         alert('Failed to retrieve states.'); 
         statesProgress.hide(); 
        } 
       }); 
      }); 
     }); 
</script> 

控制器:

public ActionResult GetStatesByCountryId(string countryId) 
     { 
      // This action method gets called via an ajax request 
      if (String.IsNullOrEmpty(countryId)) 
       throw new ArgumentNullException("countryId"); 

      var country = GetCountryById(Convert.ToInt32(countryId)); 
      var states = country != null ? GetStatesByConutryId(country.Id).ToList() : new List<StateProvince>();//Get all states by countryid 
      var result = (from s in states 
          select new { id = s.Id, name = s.Name }).ToList(); 

      return Json(result, JsonRequestBehavior.AllowGet); 
     } 
0

试试这个,

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#Country").change(function() { 

      var Id = $("#Country").val(); 
      $.ajax({ 
       url: '@Url.Action("GetCustomerNameWithId", "Test")', 
       type: "Post", 
       data: { Country: Id }, 
       success: function (listItems) { 
        var STSelectBox = jQuery('#state'); 
        STSelectBox.empty(); 
        if (listItems.length > 0) { 
         for (var i = 0; i < listItems.length; i++) { 
          if (i == 0) { 
           STSelectBox.append('<option value="' + i + '">--Select--</option>'); 
          } 
          STSelectBox.append('<option value="' + listItems[i].Value + '">' + listItems[i].Text + '</option>'); 

         } 

        } 
        else { 
         for (var i = 0; i < listItems.length; i++) { 
          STSelectBox.append('<option value="' + listItems[i].Value + '">' + listItems[i].Text + '</option>'); 

         } 
        } 
       } 


      }); 

     }); 
}); 
</script> 

查看

@Html.DropDownList("Country", (SelectList)ViewBag.country, "--Select--") 
    @Html.DropDownList("state", new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "-- Select --") 

控制器

public JsonResult GetCustomerNameWithId(string Country) 
     { 
      int _Country = 0; 
      int.TryParse(Country, out _Country); 
      var listItems = GetCustomerNameId(_Country).Select(s => new SelectListItem { Value = s.CountryID.ToString(), Text = s.CountryName }).ToList<SelectListItem>(); 
      return Json(listItems, JsonRequestBehavior.AllowGet); 
     }