简体   繁体   English

如何在ASP.NET MVC中从CSV文件级联dropdownlist

[英]how to cascade dropdownlist from csv file in asp.net mvc

I have a CSV file which looks like this: 我有一个看起来像这样的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 
(...)

And i have to make 3 dropdownlists where first I select Szerokosc then from my selected value is create dropdownlist profil and last is srednica . 我不得不做出3个dropdownlists其中第一我选择Szerokosc然后从我选择的值创建dropdownlist profil和最后一个是srednica I don't know why my approach isn't working. 我不知道为什么我的方法行不通。

This is my Controller : 这是我的控制器:

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));

  }

And this is my View: 这是我的观点:

     @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>

Any ideas on how to make this work? 关于如何进行这项工作的任何想法?

I like using a dictionaries for tasks like this. 我喜欢对这样的任务使用字典。 The code below loads the dictionaries from the csv. 下面的代码从csv加载字典。 then for selection youi just have to look up values in the dictionaries 然后,您只需选择字典中的值即可进行选择

            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);
                }

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM