I am using JavaScript.
I make an api call to get a list of class objects (asp.net mvc c#).
I enumerate through this list in JavaScript and set and add items to my dropdown.
When I click a button I want to get the selected item and parse/read/cast back the class object but I get an undefinded error.
This is my model:
public class SubscriptionLookUp
{
public string SubscriptionType { get; set; }
public int SubscriptionTypeId { get; set; }
public double UnitCost { get; set; }
public string ErrorMessage { get; set; }
}
This is my ajax call:
$.ajax({
url: "https://mydomain/SubscriptionLookUp?currentSubscription"
type: "GET",
crossDomain: true,
dataType: 'jsonp',
jsonp: 'callback',
success: function (data) {
$("#cboAvailableSubscriptions").empty();
$("#cboAvailableSubscriptions").append($("<option></option>").val('').html('Please Select'));
$.each(data, function (key, item) {
if (item.ErrorMessage !== '' && item.ErrorMessage!==null) {
$('#divError').html('Server Error');
return;
}
$("#cboAvailableSubscriptions").append($("<option></option>").val(item).html(item.SubscriptionType));
}) },
error: function (jqXHR, textStatus, errorThrown) {
$("#divError").html('Server Error');
}
});
Which is populated just fine and this is my final bit to read teh object back:
$('#divChange').click(function () {
var SubscriptionLookUp = {
SubscriptionType: '',
SubscriptionTypeId: '',
UnitCost: '',
};
SubscriptionLookUp = $("#cboAvailableSubscriptions").val();
alert(SubscriptionLookUp.SubscriptionType);
});
The alert returns undefined...
alert(SubscriptionLookUp)will give you the valueSubscriptionLookUp.SubscriptionType = $("#cboAvailableSubscriptions").val()