0

How do I get values from model using Ajax and assign these values in an array on the client?

Here is my controller

public ActionResult Oku()
{
    var query = from table in db.news where table.image_name select table;
    return Json(query,JsonRequestBehavior.AllowGet);
}

My Ajax script is:

$.ajax({
    type: "get",
    url: "Home/Oku",
    data: {},
    dataType: "json",
        // Some codes to assign array
    }
});

Thanks for your help

4

3 回答 3

1

Just implement a success callback in your Ajax call. Also you don't need to specify get, it is the default behavior.

$.ajax({
    url: "Home/Oku",
    dataType: "json",
    success: function(resp) {             
         // do something with resp object which is an array 
    }
});
于 2012-08-03T16:45:11.583 回答
0

I'd use JsonResult instead of an ActionResult.

Here's a Tutorial

于 2012-08-03T16:02:13.700 回答
0

The data returned from action is already in array format, since the query result is an IEnumerable.

You need to implement success callback in the ajax.

$(function () {
        $.ajax({
            type: "get", url: "Home/Oku", data: {}, dataType: "json", 
            success: function (data) {
               alert(data[0])
            }
        });
    }) 
于 2012-08-03T16:06:13.503 回答