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

3 Answers 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 
    }
});
Sign up to request clarification or add additional context in comments.

Comments

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])
            }
        });
    }) 

Comments

0

I'd use JsonResult instead of an ActionResult.

Here's a Tutorial

3 Comments

Actually ActionResult is the base of JsonResult. If you return Json({..}) with an ActionResult it should still work.
Nice to know...I think I'd still use it for coding clarity.
Ya there is one situation where I find it useful to use the generic ActionResult. If you have a method that may return either a JsonResult or possibly something else (ViewResult, RedirectResult, etc) in case of an error or some special case.

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.