[RESOLVED]Break JSON Result

Hello,

Below is my code in Controller.

string[] AssetNames;
AssetNames = Assets.Select(a => a.AssetName).ToArray();
return this.Json(AssetNames, JsonRequestBehavior.AllowGet);

Here is the code in Js file

 $.ajax({
        type: "POST",
        url: "Assets",
        data: data,
        traditional: true,
        dataType: "json",        
        success: function (result) {
            if ($.trim(result)) {                       
                 var count = result.length;     
                 var strMessage ="";          
                 for (var i = 0; i < result.length; i++) {
                     strMessage.append("n" + result[i])
                 }                 
                 alert(strMessage);
            }
        },
        error: function () {

        }
    }); 

I am getting the Names correctly in result. When I am assigning the each name, I am getting the error.

What would be the problem ?

Hi,

Not telling what is "the error" so that can understand "the problem" and tell you what is "the fix"?! ;-)

It’s not even Crystal clear about which assigment you are talking about and if this is a server or client side error…

Please always include the error message (or describe the bad behavior) and on which line it happens.

PatriceSc

Not telling what is "the error" so that can understand "the problem" and tell you what is "the fix"?! ;-)

Hello,

This is error in Javascript. if you observe the below for loop, there is no attribute name after result[i]. 

 for (var i = 0; i < result.length; i++) {
                     strMessage.append("n" + result[i])
                 }  

Do you mean that the code works while you would have expected to have to use result[i].name ? Could you please just describe what happens?

PatriceSc

Do you mean that the code works while you would have expected to have to use result[i].name ? Could you please just describe what happens?

In the controller code, there is no attribute. So, my question is how can we get the value and what attribute we have to give ?

Is there any default attribute ?

Server side AssetNames is an array of string so client side you have an array of string. If you return an array of object then client side you would have an array of objets with their "properties".

Leave a Reply