Multiple Dropdownlist

Asked

Viewed 19 times

0

After doing a lot of research in the documentation and checking right here at Stackoverflow, I decided to ask.

My question is the following I created two Dropdownlists.

View as it is now:

    <div>
        @Html.DropDownList("TradeItems", String.Empty)

        &nbsp;
        @Html.DropDownList("CoinItems", String.Empty)

        &nbsp;
    </div>

I already sent the data from the list I created through the controller and I’m trying to recover the selected values.

My Controller:

public IActionResult Index(int? value, int? value2)
    {
        Junction junction = new Junction();

        ViewBag.TradeItems = junction.FirstItems.ToList();
        ViewBag.CoinItems = junction.SecondItems.ToList();


        if (value != null)
        {


            switch (Convert.ToInt32(value))
            {
                case 1:
                    apiCall.getName1(1);
                    break;
                case 2:
                    apiCall.getName1(2);
                    break;
                case 3:
                    apiCall.getName1(3);
                    break;
                case 4:
                    apiCall.getName1(4);
                    break;

                case 5:
                    apiCall.getName1(5);
                    break;
                case 6:
                    apiCall.getName1(6);
                    break;
            }
        }
        if (value2 != null)
        {


            switch (Convert.ToInt32(value2))
            {
                case 1:
                    apiCall.getName2(1);
                    break;
                case 2:
                    apiCall.getName2(2);
                    break;
                case 3:
                    apiCall.getName2(3);
                    break;
                case 4:
                    apiCall.getName2(4);
                    break;

                case 5:
                    apiCall.getName2(5);
                    break;
                case 6:
                    apiCall.getName2(6);
                    break;
            }
        }

        return View();
    }

I tried this way but it didn’t work for the two dropdownlists.

View as it was before:

    <div>
        @Html.DropDownList("TradeItems", null, 
            new { onchange = "document.location.href='/Home/Index?value=' + this.value; " })
        &nbsp;
        @Html.DropDownList("CoinItems", null, 
            new { onchange = "document.location.href='/Home/Index?value2=' + this.value2; " })
    </div>

Any idea how I can retrieve the values of the two selections separately ?

  • just a hint, instead of switch could not be apiCall.getName1(Convert.ToInt32(value2))? better use a variable, but only for example. About the question, pq does not pass this.value in both, and passes another parameter telling from which dropdown comes the value?

  • And instead of Location.href... the ideal is for you to use an ajax request. And study a little more about Asp.net, mvc and web api

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.