1

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...

5
  • alert(SubscriptionLookUp) will give you the value Commented Feb 17, 2016 at 18:44
  • @EhsanSajjad thanks for your comment but that will return the whole object. i am trying to read the property from that object Commented Feb 17, 2016 at 18:46
  • you have to construct your object your self Commented Feb 17, 2016 at 18:50
  • SubscriptionLookUp.SubscriptionType = $("#cboAvailableSubscriptions").val() Commented Feb 17, 2016 at 18:51
  • @EhsanSajjad thanks but that did not work. Just to confirm the 'item' i am adding is the class object. Commented Feb 17, 2016 at 18:54

2 Answers 2

2

$.val() uses Object.prototype.toString.call(value), which is converting your item to the string [Object object] (you can see this by viewing the source of your generated html). So, when trying to read the property SubscriptionType, JavaScript is referencing the String.prototype, not your item -therefore undefined.

With little change to your code, you could use JSON.stringify and JSON.parse:

Writer

$("#cboAvailableSubscriptions").append(
  $("<option></option>").val(JSON.stringify(item))
                        .html(item.SubscriptionType)
);

Reader

SubscriptionLookUp = JSON.parse($("#cboAvailableSubscriptions").val());

You may want to look into $.data(), seems like the right tool for the job.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use data() instead of val():

$("#cboAvailableSubscriptions").append($("<option></option>").data("itemKey", item).html(item.SubscriptionType));

And to retrieve it:

$('#divChange').click(function () {

    var SubscriptionLookUp = {
        SubscriptionType: '',
        SubscriptionTypeId: '',
        UnitCost: '',
    };
    SubscriptionLookUp = $("#cboAvailableSubscriptions").find(":selected").data("itemKey");

    alert(SubscriptionLookUp.SubscriptionType);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.