I am sorry if this has been asked before but I've searched and failed to come up with a solution.
I'm displaying a list
of data in my view and in each row of the list are 2 dropdownlists
one for category and the other for item. The 2nd dropdown
is populated based on what is selected in the first dropdown
. This only works for the first row in the list. How can I make this work for each row in the list?
Here is my View:
@for (int j = 0; j <= 9; j++)
{
<tr>
<td>
<input type="checkbox" name="i" value="@j">@(j + 1)</td>
<td>
@Html.DropDownList("dropdownCountry", new SelectList(string.Empty, "Value", "Text"), "Please select a Category", new {@style = "width:250px;"})
</td>
<td>
@Html.DropDownList("dropdownState", new SelectList(string.Empty, "Value", "Text"), "Please select an Item", new {@style = "width:250px;"})
</td>
<td>
<input size="10" type="text" name="quantity" style="width: 100px"/></td>
<td>
<input size="10" type="text" name="unitofMeasure" style="width: 100px" maxlength="10"/></td>
<td>
<input size="10" type="text" name="packsize" style="width: 100px"/></td>
<td>
<input size="10" type="text" name="unitPrice" style="width: 100px"/></td>
</tr>
}
<tr>
<td></td>
<td>
<input type="submit" value="Create" name="Command" /></td>
<td></td>
<td></td>
</tr>
</table>
And here is my Script:
@section Scripts
{
<script src="~/scripts/jquery-1.8.2.min.js"></script>
<script type="text/javascript">
$(function() {
$.ajax({
type: "GET",
url: "/ProcurementRequistion/GetItemcategories",
datatype: "Json",
success: function(data) {
$.each(data,
function(index, value) {
$('#dropdownCountry')
.append('<option value="' + value.Code + '">' + value.Description + '</option>');
});
}
});
$('#dropdownCountry')
.change(function() {
$('#dropdownState').empty();
$.ajax({
type: "POST",
url: "/ProcurementRequistion/GetItemsByItemCategoryCode",
datatype: "Json",
data: { itemCategoryCode: $('#dropdownCountry').val() },
success: function(data) {
$.each(data,
function(index, value) {
$('#dropdownState')
.append('<option value="' + value.No + '">' + value.Description + '</option>');
});
}
});
});
});
</script>
}