-1

I have a dropdownlist contains a list of products which binds from database. I want to load selected product Data.

here is my code in view

     @Html.DropDownListFor(model => model.ProductName, new SelectList(Model.ProductsList, "ProductID", "ProductName"),  new
                       {
                           id = "Productslist",
                           @class = "GreenDropDownListStyle",
                           @datastyle = "dropdown",
                           @onchange = "FillProduct()"
                       })

I had use java Script code to send select Product Id then get it's data

<script type="text/javascript">
        $(document).ready(function () {

            $("#Productslist").change(function () {
                debugger;
                    var selectedIndex = $(this).val();
                    var divH = document.getElementById(selectedIndex);
                    if (selectedIndex > 0) {
                        $.ajax(
                            {
                                url: '/Inventory/ViewProduct',
                                type: 'GET',
                                data: { ProductID: selectedIndex },
                                contentType: 'application/json; charset=utf-8',
                                success: function (data) {
                                    ///
                                },
                                error: function () {
                                    alert("error");
                                }
                            });
                    }
                    else {
                        divH.style.visibility = 'hidden';      // Hide
                    }
                });
            });
    </script>

the issue that this code send the selected index not the is for the selected product also its send the id as a string so it never go to my condition last thing in case of succeed what i should write

shaymaa adel
  • 85
  • 1
  • 1
  • 6
  • Look at this: https://stackoverflow.com/questions/2780566/get-selected-value-of-a-dropdowns-item-using-jquery as for what to do with the data after, that depends on where you want to put it, and what data gets returned in what format etc.. If you're not sure use your debugger in browser to see what gets returned. – Danimal Jun 07 '18 at 14:33
  • `@onchange = "FillProduct()` _and_ `$("#Productslist").change`...so you've got two different JS functions running on the same event, is that right? You've only shown us one of them, and I think you probably only need one. Maybe remove the "onchange" code if it's not needed. – ADyson Jun 07 '18 at 14:40
  • i will $("#Productslist").change – shaymaa adel Jun 07 '18 at 14:42
  • Anyway... "this code send the selected index not the is for the selected product"...I don't think so, `$(this).val()` will get the selected _value_ of the dropdown. i.e. if you have an option `` and this item is currently selected, then `$(this).val()` will return "ABC" - so the value, not the index. And I don't think `var divH = document.getElementById(selectedIndex);` will ever work because ` – ADyson Jun 07 '18 at 14:42
  • i know that send selected index not ID and this is the issue – shaymaa adel Jun 07 '18 at 14:44
  • prove it please - show us some sample HTML generated by the DropdownListFor, and what item you selected, and what the result of `$this.val()` was in that situation. I don't see how you can be right, unless you have a different meaning of "selected index" than me - I think it means the the number which shows where the item is in the dropdownlist - e.g. index 1 would be the 2nd option in the (0-based) list, but that might not be the _value_ of that option – ADyson Jun 07 '18 at 14:46
  • "also its send the id as a string so it never go to my condition "... assuming `selectedIndex` is actually potentially a number, then `if (parseInt(selectedIndex) > 0)` would solve that pretty easily. – ADyson Jun 07 '18 at 14:48
  • ok the dropdown list binds from database List AllProductList = new List(); DBProduct TheModel = new DBProduct(); AllProductList = TheModel.GetAllProducts(); ProductClass Parameter = new ProductClass() { ProductsList = AllProductList }; return View(Parameter); this list contains id , name ,...etc what i target is send id of the selectedindex – shaymaa adel Jun 07 '18 at 14:49
  • "in case of succeed what i should write"...we don't know because you haven't said what the response from the ajax call is, or exactly what you want to do with it – ADyson Jun 07 '18 at 14:49
  • in case of succeed i need to get selected product details – shaymaa adel Jun 07 '18 at 14:54
  • ok well write some code to do that! I still don't know what these product details look like, how they're contained in the ajax response or what you want to do with them once you've got them! Not really sure what you want anyone to say to such a vague statement. I don't have a view of your screen, or your mind. Anyway let's stick to the first part about the selectedIndex... – ADyson Jun 07 '18 at 14:56

1 Answers1

0

it solved by

var ProductID = this.value;
shaymaa adel
  • 85
  • 1
  • 1
  • 6