Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello

I have function in my Controller and I have fetch data from data base in string format. the result is :

[{"NationalCode":"12458","LicenceCode":"4582aS","Phone":"45236987"}]

that i pass it to my JavaScript file. How can access to the fields of this string.
I mean access to "NationalCode" or "LicenceCode" or "Phone" values ?

Thanks in Advance

What I have tried:

1)
JavaScript
var source = JSON.parse(response);
alert(source.NationalCode);  // returns undefined 


2)
JavaScript
var source = JSON.parse(response);
alert(source["NationalCode"]);  // returns undefined 


3)
JavaScript
alert(response); // returns the whole string


4)
JavaScript
var source = JSON.parse(response);
alert(source); // returns [object object]



response is the result that has returned from controller function
Posted
Updated 8-Nov-16 18:46pm

1 solution

Hello,

Re: [{"NationalCode":"12458","LicenceCode":"4582aS","Phone":"45236987"}]

You can direct access it using:

View:

success: function (response) {
var groupVal = response.NationalCode;
}

Let me explain you:

Controller:

C#
public JsonResult GetAdminSettingByID(int settingID)
        {
            var adminSettingDetails = bjClsAdminService.GetAdminSettingByID(settingID);
            return Json(adminSettingDetails, JsonRequestBehavior.AllowGet);
        }


Using model class for create json and pass the class object directly here.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900