who-aditya-nawandar
who-aditya-nawandar

Reputation: 1242

Unable to read a list in Jquery

I am trying to read (access values) a list in Jquery like this:

    var list = { length: 0 };
    var subTypeId = "";
    list = '<%: Model.lstDC_TAn %>';
    alert('list = ' + list);
    for (var j = 0; j < list.length; j++) {
    subTypeId = list[j].DCSubTypeId;
    alert('subTypeId = ' + subTypeId)

The alert for list shows that its a "Generic list", however, the subTypeId is always undefined. I have checked in the controller. The list is not null and the property that I am trying to access (DCSubTypeId) is also present inside each element in the list.

EDIT:

 public List<DC_TAn> lstDC_TAn { get; set; }

Upvotes: 0

Views: 204

Answers (1)

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

Your logic is flawed. As it stands, you're dealing with raw string. Such code:

list = 'Generic list';
alert(list[0]);

Will alert "G", as string is array of characters. Character/String in JavaScript indeed has no property called "DewCardSubTypeId".

You need to first change the server side code to output proper object, e.g.

public string lstDewCard_TypeAssociation
{
    get
    {
        return "[{\"DewCardSubTypeId\": 5}, {\"DewCardSubTypeId\": 100}]";
    }
}

Then get rid of the quotes when assigning it:

list = <%: Model.lstDewCard_TypeAssociation %>;

Then your code should work.

Upvotes: 1

Related Questions