2013-02-26 43 views
0

在下面我initData是ID爲#秒的國家名稱。

我想要做的是改變了國家,得到了市名列表,它我曾經在一個類中實現和它的作品,但在下拉變化,我不知道怎麼回來後,讓這些城市:(

任何幫助,將不勝感激。

感謝

<select class="Countries"></select> 


    <script type="text/javascript"> 
    $(document).ready(function() { 
     var initData= @Html.Raw(Json.Encode(Model)) 

     $(function() { 
      var $select = $('.Countries'); 
      $(".Countries").append("<option value='0'>--Select Country--</option>"); 
      $.each(initialData, function(i, country) { 
       var option = $('<option>', { 
        value: country.CountryID 
       }).html(country.CountryName).appendTo($select); 

      }); 
     }); 

     $('.Countries').change(function() { 
      var postData = []; 
       alert(postData); 
       postData.push({value: $(this).val()}); 
       $.post('@Url.Action("Cities")', postData, function (data) { 
        if (data.success) { 
         alert('Retrieve list of cities'); 
        } 

       }, 'json'); 

     }); 
    }); 
    </script> 

我的控制器:

 public ActionResult Cities(int id) 
    { 
     List<CityModel> listCity = _da.GetCities(id); 
     return RedirectToAction("Index"); 
    } 
+1

你有沒有調試代碼?你$ .post()是否會觸發?它是否到達城市()? Cities()是否返回Json? – Dima 2013-02-26 18:42:13

+0

不,它不會那麼遠。不超越postData.push。 – NoviceDeveloper 2013-02-26 18:49:38

+0

POSTDATA 的ReferenceError:POSTDATA沒有定義 – NoviceDeveloper 2013-02-26 19:01:47

回答

0

我的ID未定義,這意味着它沒有相關的名稱。 postdata只包含Value .. ..添加ID後它工作。

postData.push({name: 'id', value: $(this).val()});