2014-01-29 72 views
0

我有一個控制器,從列在數據庫中搜索值,並把它放在一個DropDownList:沒有使用模型級聯DROPDOWNLIST MVC

 public ActionResult Index() 
     { 

      var listPro = db.P1_PRODUTOS.Select(c => new { c.id_produtos, c.sgl_produtos }); 
      ViewBag.Produtos = new SelectList(listPro.AsEnumerable(), "id_produtos", "sgl_produtos"); 


      return View(); 
     } 

在View:

th>Produtos: @Html.DropDownList("Produtos", (SelectList)ViewBag.CategoryId, "Selecione o Produto")</th> 

是否正常一般。

如何啓用另一個DropDownList,並將其他表中的值與第一個DropDownList的選擇相關聯。

回答

0
check this 
im doing that just now see my code 
$("#Container").on("change", "select", function() { 
      var url = '/Controller/Action/Get'; 
      $.ajax({ 
       url: url, 
       cache: true, 
       type: "POST", 
       data: { 'example': $('#example').val() }, 
       success: function (data) { 
        var items = $('#Dropdown'); 
        items.empty(); 
        $.each(data, function (i, drdvalue) { 
         items.append(
         $('<option/>', { 
          value: drdvalue.Text, 
          html: drdvalue.Value 
         }) 
        ); 
0

我有這樣一個問題,我無法以通爲下拉值,

<script type="text/javascript" language="javascript"> 
    $(document).ready(function() { 
     $('#Rod').change(function() { ChangeRod(); }); 
    }); 

    function ChangeRod() { 
     var selectedValue = $('#Rod option:selected').val(); 
     if ($.trim(selectedValue).length > 0) { 
      GetLevant(selectedValue); 
     } 
    } 

    function GetLevant(id_Rod) { 
     $.ajax(
     { 
      url: '@Url.Action("Levant", "GraficosLevants")', 
      data: { id_Rod: id_Rod }, 
      type: "POST", 
      cache: false, 
      async: false, 
      dataType: 'json', 
      sucess: function (data) 
      { 
       if (data.length >= 0) 
       { 
        $('#Levant').empty(); 
        $('#Levant').append($('<option></option>').val('').text('Selecione o Levant')); 
        $.each(data, function (i, item) 
        { 
         $('#Levant').append(('<option></option>').val(item.key).text(item.Value)); 
        }); 

       } 
      } 
     }); 
    } 
</script> 

與控制器下面的類:

[HttpPost] 
     public JsonResult Levant(string id_Rod) 
     { 
      List<KeyValuePair<string, string>> items = new List<KeyValuePair<string, string>>(); 

      if (!string.IsNullOrWhiteSpace(id_Rod)) 
      { 
       var Levants = this.GetLevant(id_Rod); 
       if (Levants.Count() > 0) 
       { 
        foreach (var Levant in Levants) 
        { 
         items.Add(new KeyValuePair<string, string>(Levant.id.ToString(), Levant.Levant)); 
        } 
       } 

      } 
      return this.Json(items); 
     } 



     private IEnumerable<XXX_Levant> GetLevant(string id_Rod) 
     { 
      int idSelectRod = Convert.ToInt32(id_Rod); 
      var query = db.XXX_Levant.Where(l => l.Rod_id == idSelectRod).OrderBy(l => l.Levant); 
      return query.ToList(); 
     } 
+0

嘗試: 'id_Rod':id_Rod – siphab

+0

我不明白在哪裏試試:'id_Rod':id_Rod – rysahara

+0

data:{'id_Rod':id_Rod}, – siphab

相關問題