0

I have the following code:

@Html.DropDownList("jenisBarang", new SelectList(ViewBag.listJenis), "Pilih Perangkat/Layanan", new { @class = "form-control", @id = "ddlJenis", @onchange = "GetMerk(this.value);", @name="jenisBarang" })
@Html.DropDownList("merkBarang", new SelectList("pilih Merk"), "Pilih Merk", new { @class = "form-control", @id = "ddlMerk", @name = "jenisBarang" })


The second Dropdownlist consists of some strings based on what I've chosen from the first dropdownlist.
ex:

  • a
  • b
  • c

When I send it to my action, it read as integer (0).
How do I get the value of the dropdownlist as a string? (ex I chose a)

edit: this is the code to manipulate the second dropdown:

<script language="javascript" type="text/javascript">
    function GetMerk(_jenis) {
        var procemessage = "<option value='0'> Please wait...</option>";
        $("#ddlMerk").html(procemessage).show();
        var url = "/Barang/GetMerkByJenis/";

        $.ajax({
            url: url,
            data: { jenisID: _jenis },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>Select Merk</option>";
                for (var x = 0; x < data.length; x++) {

                    markup += "<option value=" + x + ">" + data[x]+ "</option>";
                }
                $("#ddlMerk").html(markup).show();
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });

    }
</script>
5
  • Can you show the code that used to manipulate the second dropdown? Commented Oct 27, 2017 at 7:42
  • From your dropdownlist (DDL) construction seems that you want to create cascading DDL, right? How you bind second DDL to the first one, and is that value of second DDL returned as integer instead of text? Commented Oct 27, 2017 at 7:50
  • I used javascript and ajax on how to show my data to DDL2 from DDL1, the code on js and ajax is added in the question Commented Oct 27, 2017 at 7:58
  • when you select a from DDL1, it sends an iteger or from DDL2 ? Commented Oct 27, 2017 at 8:08
  • You can see an example here: Creating Simple Cascading DropDownList In MVC 4 Using Razor. Note that you need to change the second argument (dataValueField) with column name which represents value passed from DDL. Commented Oct 27, 2017 at 8:17

3 Answers 3

1

On your JS code, in the loop, you are binding to select option's value your iteration variable - x, which is number, so it will pass integer to the controller. If you want to pass text, you should bind to value data[x] not x:

. . .
for (var x = 0; x < data.length; x++) {
    markup += "<option value=" +  data[x] + ">" + data[x]+ "</option>";
}
. . .
Sign up to request clarification or add additional context in comments.

5 Comments

the problem is on the second dropdownlist. Whatever I chose at the second dropdownlist, it came out as integer in the model. On the other hand, the first dropdownlist gave me string result (which is what I wanted)
How you've initialized your second DropDown list's SelectList with string parameter? There is no such overload in SelectList.
I was aiming to delete that parameter, but there was error when I did that, so I inserted a string there, and it actually works just find with the default option set as char-by-char ("p", "i", "l", ..., "k")
I've tried SelectList witch one string parameter, it breaks down string to chars and binds to select element. Also I haven't found any problem with that, it returns strings to controller.
I've worked my js just like you told me, and it actually works! Big thanks to you!
1

You are posting an integer to server. Therefore your controller received integer value for second dropdown, let's take a look at your code

var markup = "<option value='0'>Select Merk</option>";
                for (var x = 0; x < data.length; x++) {

                    markup += "<option value=" + x + ">" + data[x]+ "</option>";
                }

If you want to get the text, it should be look like

var markup = "<option value=''>Select Merk</option>";
                for (var x = 0; x < data.length; x++) {

                    markup += "<option value=" + data[x] + ">" + data[x]+ "</option>";
                }

1 Comment

oh yeah it works, I didn't know the integer passed because of the "option value" field. Thanks for your help Edward!
0

There is a problem with your javascript, Incase if you want to get the dropdown text in your action method
Please replace your code with the followig

<script language="javascript" type="text/javascript">
function GetMerk(_jenis) {
    var procemessage = "<option value='0'> Please wait...</option>";
    $("#ddlMerk").html(procemessage).show();
    var url = "/Home/GetMerkByJenis/";

    $.ajax({
        url: url,
        data: { jenisID: _jenis },
        cache: false,
        type: "POST",
        success: function (data) {
            var markup = "<option value='0'>Select Merk</option>";
            for (var x = 0; x < data.length; x++) {

                markup += "<option value=" + data[x] + ">" + data[x] + "</option>";
            }
            $("#ddlMerk").html(markup).show();
        },
        error: function (reponse) {
            alert("error : " + reponse);
        }
    });

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.