Reputation: 99
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:
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>
Upvotes: 0
Views: 627
Reputation: 525
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);
}
});
}
Upvotes: 0
Reputation: 7213
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>";
}
. . .
Upvotes: 1
Reputation: 997
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>";
}
Upvotes: 1