2017-10-31 3 views
1

のasp.net MVCでcsvファイルからドロップダウンリストにカスケードしますは、どのように私はこのようになりますCSVファイル持って

Srednica,Profil,Szerokosc 
17.00,60.00,120.00 
17.00,70.00,120.00 
17.00,80.00,130.00 
18.00,80.00,130.00 
13.00,70.00,135.00 
(...) 

を、私は私の選択した値であるから、私はその後、Szerokoscを選択するには、最初の3 dropdownlistsをしなければなりませんdropdownlistprofilを作成し、最後はsrednicaです。私のアプローチがなぜ機能していないのか分かりません。

public DataTable ReadCSVFile (string pathCSV, DataTable dataTable) 
     { 
      string[] contentFile = File.ReadAllLines(pathCSV); 
      if (contentFile.Count() > 0) 
      { 
       string[] col = contentFile[0].Split(','); 

       for (int i = 0; i < col.Count(); i++) 
       { 
        dataTable.Columns.Add(col[i]); 
       } 

       for (int i = 1; i<contentFile.Count(); i++) 
       { 
        string[] row = contentFile[i].Split(','); 
        dataTable.Rows.Add(row); 
       } 
      } 
      return dataTable; 
     } 

    public ActionResult Index() 
     { 
      ReadCSV readCSV = new ReadCSV(); 
      readCSV.ReadCSVFile(pathCSV, dataTable); 

      for (int i = 0; i < dataTable.Rows.Count; i++) 
      { 
       var selectedlistitem = new SelectListItem { Text = dataTable.Rows[i]["Szerokosc"].ToString(), Value = dataTable.Rows[i]["Szerokosc"].ToString() }; 
       if (!listSzerokosc.Any(l => l.Text == selectedlistitem.Text)) 
       { 
        listSzerokosc.Add(selectedlistitem); 
       } 
      } 
      ViewBag.Szerokosc = listSzerokosc; 

      return View(); 
     } 

     public JsonResult GetProfil(string value) 
     { 
      List<SelectListItem> listProfil = new List<SelectListItem>(); 
      for (int i = 0; i < dataTable.Rows.Count; i++) 
      { 
       if (value != dataTable.Rows[i]["Profil"].ToString()) 
        continue; 
       var selectedlistitem = new SelectListItem { Text = dataTable.Rows[i]["Profil"].ToString(), Value = dataTable.Rows[i]["Profil"].ToString() }; 
       if (!listProfil.Any(l => l.Text == selectedlistitem.Text)) 
       { 
        listProfil.Add(selectedlistitem); 
       } 
      } 
      return Json(new SelectList(listProfil, "Value", "Text", JsonRequestBehavior.AllowGet)); 

    } 

そして、これは私のビューです:

これは私のコントローラでこの作業を行う方法について

 @using (Html.BeginForm()) 
{ 
    @Html.DropDownList("Szerokosc", ViewBag.Szerokosc as SelectList, "---Select szerokosc---", new { @id = "MainDropDownListID" }) 
    @Html.DropDownList("Profil", new SelectList(string.Empty, "Value", "Text"), "---Select profil---", new { style = "width:250px" }) 
    @Html.DropDownList("Srednica", new SelectList(string.Empty, "Value", "Text"), "---Select Srednice---", new { style = "width:250px" }) 
} 


<script src="~/Scripts/jquery-1.10.2.min.js"></script> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#Szerokosc").change(function() { 
      $("#Profil").empty(); 
      $.ajax({ 
       type: 'POST', 
       url: '@Url.Action("GetProfil")', 
       dataType: 'json', 
       data: { id: $("#Szerokosc").val() }, 
       success: function (profile) { 
        $.each(profile, function (i, profil) { 
         $("#Profil").append('<option value="' + profil.Value + '">' + 
          profil.Text + '</option>'); 
        }); 
       }, 
      }); 
      return false; 
     }) 
    }); 
</script> 

任意のアイデア?

答えて

0

私はこのようなタスクの辞書を使いたいです。以下のコードは、csvから辞書を読み込みます。選択のために辞書の値を検索するだけでいいです

  Dictionary<decimal, Dictionary<decimal, decimal>> srednicaDict = new Dictionary<decimal, Dictionary<decimal, decimal>>(); 



      string input = "17.00,60.00,120.00\n" + 
          "17.00,70.00,120.00\n" + 
          "17.00,80.00,130.00\n" + 
          "18.00,80.00,130.00\n" + 
          "13.00,70.00,135.00"; 

      StringReader reader = new StringReader(input); 
      string line = ""; 
      while ((line = reader.ReadLine()) != null) 
      { 
       decimal[] data = line.Split(new char[] { ',' }).Select(x => decimal.Parse(x)).ToArray(); 
       Dictionary<decimal, decimal> srednica = null; 
       decimal szerokosc = 0; 
       if (srednicaDict.ContainsKey(data[0])) 
       { 
        srednica = srednicaDict[data[0]]; 
        KeyValuePair<decimal, decimal> profil; 
        if (srednica.ContainsKey(data[1])) 
        { 
         szerokosc = srednica[data[1]]; 
         if (data[2] == szerokosc) 
         { 
          Console.WriteLine("Data Already in dictionary"); 
         } 
         else 
         { 
          srednica.Add(data[1], szerokosc); 
         } 
        } 
        else 
        { 
         srednica.Add(data[1],data[2]); 
        } 
       } 
       else 
       { 
        Dictionary<decimal, decimal> newProfil = new Dictionary<decimal, decimal>(); 
        newProfil.Add(data[1], data[2]); 
        srednicaDict.Add(data[0],newProfil); 
       } 

      } 
関連する問題