2017-06-11 4 views
0

私はそれまで完全に動作するカスケードドロップダウンを持っています。今度は、以下に示すようにテキストボックスに値を設定する必要があります。別のAJAX呼び出しを起動せずに、サブフィールドに値を設定して再設定できますか?

Cascading Dropdowns

Secondary text fields based on Select Species Dropdown

AJAXコード

$('#CommonHerpsDropdown').change(function() { 
     $('#SpeciesDropdown').empty(); 

     $.ajax({ 
      type: "POST", 
      url: '@Url.Action("GetGenericSpeciesByCommonId")', 
      datatype: "Json", 
      data: { cAnimalId: $('#CommonHerpsDropdown').val() }, 
      success: function (genericSpecies) { 
       $.each(genericSpecies, function (index, value) { 
        $('#SpeciesDropdown').append('<option value="' + value.SpeciesId + '">' + value.StandardName + '</option>'); 
        $('#ScientificClass').val(value.ScientificClassification); 
        $('#ScientificOrder').val(value.ScientificOrder); 
        $('#ScientificFamily').val(value.ScientificFamily); 
        $('#ScientificGenus').val(value.ScientificGenus); 
        $('#ScientificSpecies').val(value.ScientificSpecies); 
       }); 
      } 
     }); 

    }); 

MVCコントローラJSON方法

public JsonResult GetGenericSpeciesByCommonId(int cAnimalId) 
     { 
      //Lets get the species data based on CommonHerpID. 
      var genericSpecies = (from x in _context.ReptileSpeciesList 
           where x.CommonHerpId == @cAnimalId 
           select x).OrderBy(x => x.StandardName); 
      return Json(genericSpecies); 
     } 

私の質問は、dbテーブルへの別の呼び出しを使用せずに種の選択が変更されたときに、分類フィールドと順序フィールドを設定して再投入する方法です。

+0

よう 何かを埋めますか?テキストボックスにも入力されているようです –

+0

コードが機能します。しかし、私は$( '#SpeciesDropdown')を避ける方法があるかどうかを調べようとしていましたが、change(functon(){// Ajax呼び出しとフィールドバインディング})、これらのテキストフィールドを複数バインドする必要はありません時間別々の変更機能 – Alex

+0

[この回答]の2番目と3番目のオプションを参照してください(https://stackoverflow.com/questions/28627421/better-way-to-load-2-dropdown-in-mvc/28640420#28640420) –

答えて

0

データリストオブジェクトをビューバックとしてビューに渡すことができます。ビューで配列オブジェクトを取得した後、配列にfilter関数を使用すると、使用したフィルタと一致する配列が得られます。その配列にforeachの機能を実行する代わりに、あなたはfrormのAJAXを取得している1と現在のコードの問題は何ですか、あなたのドロップダウンこの

//ViewBag.genericSpecies is coming from _context.ReptileSpeciesList without applying any filter 
// I use json.encode to encode your list into json object 
    var AllGenericSpecies= @Html.Raw(Json.Encode(ViewBag.genericSpecies)); 
//then onchange 
$('#CommonHerpsDropdown').change(function() { 
     $('#SpeciesDropdown').empty(); 
var HerpID = $('#CommonHerpsDropdown').val(); 
//filter all generic species, apply the filter 
AllGenericSpecies.filter(function(spiece){ 
return spiece.CommonHerpId == HerpID; 
}).forEach(function(value) { 
( 
$('#SpeciesDropdown').append('<option value="' + value.SpeciesId + '">' + value.StandardName + '</option>'); 
        $('#ScientificClass').val(value.ScientificClassification); 
        $('#ScientificOrder').val(value.ScientificOrder); 
        $('#ScientificFamily').val(value.ScientificFamily); 
        $('#ScientificGenus').val(value.ScientificGenus); 
        $('#ScientificSpecies').val(value.ScientificSpecies); 
//end of foreach   
)}; 
//end of on change listener 
)}); 
関連する問題