Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9212397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:30:58+00:00 2026-06-18T01:30:58+00:00

I can`t get json object. My object is: public class Person { public string

  • 0

I can`t get json object.

My object is:

public class Person
{
   public string firstName { get; set; }
   public string lastName { get; set; }        
}

PersonsControll:

    public ActionResult Index()
    {
        return View();
    }

    // GET: /Persons/GetPerson
    //[AcceptVerbs(HttpVerbs.Get)]
    //[OutputCache(Duration = 0, VaryByParam = "*")]
    public JsonResult GetPerson()
    {
        Person p = new Person { firstName = "Jonas", lastName = "Antanaitis" };
        return Json(p, JsonRequestBehavior.AllowGet);
    }

My index view:

@Scripts.Render("~/bundles/knockout")

<p>First name: <strong data-bind="text: firstName"></strong></p>
<p>Last name: <strong data-bind="text: lastName"></strong></p>

<div id="dispJson"></div>
<script type="text/javascript">                
    var data = $.getJSON(".../GetPerson", function (result) {
        //state = result.readyState;
        firstName: result.firsName;
        lastName: result.lastName;
    })    
    .error(function () { alert("error"); });    

    function viewModel() {
        ko.mapping.fromJS(data);
        //state = data.readyState;
        //firstName = data.firstName;
        //lastName = data.lastName;
    };    
    document.write(JSON.stringify(data)); //this line prints  "{"readyState":1}"

    ko.applyBindings(new viewModel());    
</script>

When I go to “…/GetPerson” in browser, I get this displayed:
{“firstName”:”Jonas”,”lastName”:”Antanaitis”}

,but when try to get data witch javascript in view – I dont`t get this data.

What I am doing wrong, why I cant get data?
Where commented code – I tried this approaches.. but nothing helped.

I tried:

*$.ajax({
    type: "GET",
    cache: false,
    url: ".../GetPerson",        
}).done(function (msg) {
    alert("Data Saved: " + msg.readyState + "  " + msg.firstName + "  " + msg.lastName);
});*

Then alert box displays: “Data Saved: undefined Jonas Antanaitis“

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T01:30:59+00:00Added an answer on June 18, 2026 at 1:30 am

    You are mixing async code with sync code… You have this line here

    var data = $.getJSON(".../GetPerson", function (result) {
    

    data will actually be the a promise (this is what the ajax returns). You do not actually have the data until the callback executes – ie result is the thing that has the ACTUAL data.

    You need to move the binding to INSIDE the callback OR use an observable that you set after the fact:

    1) Inside the callback example

    <script type="text/javascript">                
        $.getJSON(".../GetPerson", function (result) {
    
            function viewModel() {
                return ko.mapping.fromJS(result);
            };
    
            ko.applyBindings(new viewModel());
        })    
        .error(function () { alert("error"); });        
    </script>
    

    2) Use an observable

    <script>
        function viewModel() {
            return { data: ko.observable() };
        };
    
        // Your html bindings will bind to data.* instead of just *
        ko.applyBindings(new viewModel());
    
        $.getJSON(".../GetPerson", function (result) {
            data(ko.mapping.fromJS(result));
        })    
        .error(function () { alert("error"); });      
    </script>
    

    And the corresponding html (The with binding makes it a bit cleaner than if):

    <!-- ko with: data -->
    <p>First name: <strong data-bind="text: firstName"></strong></p>
    <p>Last name: <strong data-bind="text: lastName"></strong></p>
    <!-- /ko -->
    

    The advantage of the second method is that you are binding everything quickly… and can then show your data as it comes in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What C-sharp type can I serialize to get JSON object with format name:[[1,2,3],[1,2,3],[1,2,3]] If
We can get class Class object by 3 methods: MyClass.class obj.getClass Class.forName(className) I don't
i am using MVC c# i have a object with public class Person {
I have the following code in a controller. class Person { public string Name
i am using MVC c# i have a object with public class Person {
Is there any method I can get the full JSON data from jqGrid when
I can connect to server and get json data. it is in this format:
How can I get the array data from a JSON sent back by my
I am trying to have this exact JSON syntax but can't get to it:
I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.