0

I have an MVC app with leaflet running in it, I am parsing xml data to get the paths to the leaflet tiles which I then display in a drop down via a ViewData["value"]. The problem is I can't seem to figure out how to get that selected value and pass it down to the leaflet js as a path and then display everything. I tried many different ways to get the selection data but I'm just hitting a wall again and again. The below code is how I send it to the view. I display it via an @Html.DropDownList("layerType", ViewData["value"] as List)

        string outputPath;
        outputPath = ConfigurationManager.AppSettings.Get("outputPath");
        XmlDocument xDoc = new XmlDocument();
        xDoc.Load(outputPath + @"\log.xml");
        XmlNodeList layerType = xDoc.GetElementsByTagName("layerType");
        XmlNodeList layerPath = xDoc.GetElementsByTagName("layerPath");

        XDocument doc = XDocument.Load(outputPath + @"\log.xml");

        var count = doc.Descendants("layers")
           .Descendants("layerData")
           .Count();


        List<SelectListItem> li = new List<SelectListItem>();
        foreach (int i in Enumerable.Range(0, count))
        {
            li.Add(new SelectListItem { Text = layerPath[i].InnerText, Value = i.ToString() });
        }
        ViewData["value"] = li;
        return View(li);

How would I get the selected data and simply pass it down into the leaflet part inside the js tags. Would I maybe pass it back into a controller and then back to the view?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michael Knight
  • 39
  • 2
  • 11

1 Answers1

0
@Html.DropDownList("layerType", 
    new SelectList((IEnumerable) ViewData["value"]), "Value", "Text")

In the Post method, you have to fill the viewdata again to avoid error if model returns invalid

Refer Below Link : MVC Select List with Model at postback, how?

Working Code

https://dotnetfiddle.net/2lrb2S

Get Value on View

 <script>
    var conceptName = $('#nameofyourdropdowncontrol').find(":selected").text(); 
    //code for passing value 
</script>
Divyesh Jani
  • 301
  • 3
  • 11
  • Hey Thanks for the reply, but I don't get the view data from a model. I get it from a list and i pass it to the viewdata. I just need to get that value and pass it into a variable, I understand that I can pass the viewdata from the view to a controller and then back as a value but I think that is not efficient. – Michael Knight Feb 05 '20 at 10:32
  • Sorry I don't understand You, I pass the the value to the html.dropdown with @Html.DropDownList("layerType", ViewData["value"] as List) which is working, but I need to get the value that I selected from the dropdownlist. – Michael Knight Feb 05 '20 at 11:06