0

I'm trying get data from database in my application,my js code looks like:

$(function () {
 $("#getNotices").button().click(function () {
     $.get("/Notice/GetItems", function (data, statusText) {
         console.log(data);
     });
 });
});

My controller:

[HttpGet]
 public IEnumerable<Notice> GetItems()
 {
    return db.GetItems();
 }

And Method GetItems:

public IEnumerable<Notice> GetItems()
{
    return _db.Notices;
}

It always returns:

enter image description here

Tell me please how i can get data, database keeps data i checked,from database? Thanks for your answers!

3
  • 1
    maybe just silly idea but have you tried to call return _db.Notices.ToList(); in GetItems()? Commented Jan 21, 2017 at 12:59
  • @JaroslavKadlec yes, I tried, it returns : "System.Collections.Generic.List'. Commented Jan 21, 2017 at 13:03
  • 1
    does it change behavior in case you'd change it in way it's described in my answer? e.g. change signature of method to return JsonResult? Then potentially try remove [HttpGet] attribute and try to acquire data via POST but that would more point to wrong configuration or something like that. Commented Jan 21, 2017 at 13:15

2 Answers 2

1

Use Json Action Results as follows:-

[HttpGet]
 public JsonResult GetItems()
 {
     return Json(db.GetItems().ToList(), JsonRequestBehavior.AllowGet);
 }
Sign up to request clarification or add additional context in comments.

1 Comment

did that answer your question @Vladimir Khodakovskey ?
1

You could try change code in your GetItems() method into:

public JsonResult GetItems()
{
    return Json(_db.Notices.ToList());
}

This way you're saying framework it should represent result as JSON string, in case you do not so then just .ToString() method is called on your result in background and then exactly this string is sent to your UI.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.