KeyboardFriendly
KeyboardFriendly

Reputation: 1798

JSON returning object Object not String

The below code is returning [object Object] not a string. How can I get it to return the correct string representation? I am using ASP.NET MVC4 C#.

public JsonResult Names(string name)
{
    var a = db.NamesToGet.Select(e => new 
    {
        name = e.Names                        
    });          
    return Json(a, JsonRequestBehavior.AllowGet);
}

Upvotes: 0

Views: 174

Answers (2)

WannaCSharp
WannaCSharp

Reputation: 1898

Access it with data.name or substitute data to your json variable name.

$.get("/Controller/GetName", function(data) {
   alert(data.name);
   //or alert(data[0].name);
});

Upvotes: 1

Van
Van

Reputation: 1387

try

var a = db.NamesToGet.Select(e=>e.Names);

Upvotes: 2

Related Questions