I've this select element of html5 or Dropdown. I'm using c# to fetch data from database which it is getting correctly. when I try to bind that data fetched from db to select element using java script it just don't do that. I can get all the UserNames in a java script variable but when I try to bind that to dropdown/select element it just does not appear
My C# Method in MVC controller is
public JsonResult GetUsersList()
{
// var userList= new List<>
var userList = _db.UserInfromations.ToList();
return Json(userList, JsonRequestBehavior.AllowGet);
}
My select element
<select id="userDropDown" required name="userDropDown"></select>
and my java script I'll add my all tries that I've don so far.. created separate function for success and all but noting works
jQuery(document).ready(function () {
// debugger;
$.ajax({
type: "GET",
url: "/User/GetUsersList",
contentType: "application/json;charset=utf-8",
dataType: "json",
})
.success(function (data) {
var userName = data.UserName;
$("#userDropDown").val(userName);
return false;
})
.error(function () {
window.ajaxErrorMessage();
});
}(jQuery));
The separate Success function
function displayUser(response) {
var getData = response;
if (getData.length > 0) {
var user = "";
// user = "";
for (var i = 0; i < getData.length; i++) {
user += " " + getData[i].UserName ;
}
// $("#userDropDown").val(user);
$("#userDropDown").text(user);
//$("#userDropDown").html(user);
}
}