Here is my script:
<script>
$(function () {
$("#selectUser").on("change", function () {
alert('button clicked');
var selectedUser = $(this).val();
if (selectedUser != null && selectedUser != '') {
$.getJSON('@Url.Action("GetAUsersRegisteredRoles")' + '/' + selectedUser, function (data) {
var dualSelect = $('#dualSelectRoles1');
dualSelect.empty();
$.each(data, function (index, data) {
dualSelect.append($('<option/>', {
value: index,
text: data.text
}));
});
});
}
}
);
});
here is the html element that I am trying to update:
<label>Select User</label>
<span class="field">
<select name="selectUser" id="selectUser">
<option value="">Choose One</option>
@foreach (var item in Model.Users)
{
<option value="@item.Id">@item.UserName</option>
}
</select>
</span>
<label>Select Roles</label>
<span class="dualselect">
<select name="RolesSelect" id="dualSelectRoles1" multiple="multiple" size="10">
<option value="">Admin</option>
<option value="">User</option>
<option value="">Technical</option>
<option value="">Sales</option>
<option value="">End User</option>
<option value="">Cient</option>
</select>
<span class="ds_arrow">
<span class="arrow ds_prev">«</span>
<span class="arrow ds_next">»</span>
</span>
<select name="select4" multiple="multiple" id="dualSelectRoles2" size="10">
<option value=""></option>
</select>
</span>
Now my script works so far:
when I click on the user select my alert popups up then it calls my action method and it returns the json.
It then clears my select element to make it blank but it then doesn't add any elements? What am I doing wrong?
[Edit]
Here is my output of console.log(JSON.stringify(data)):
["Admin","Technical"]
console.log(JSON.stringify(data))and add the result to the question.datais an array of strings, it will not have a property.text