I am trying to create an api for a movie guide mobile application, now i need to return json to the user containing information about the movie.
my request url is
/mobile/details/{id}
following is the controller:
public ActionResult Details(int id)
{
return View(kr.GetMovie(id));
}
GetMovie(id) returns an object of type Movie to the view which contains all the info;
asked Apr 11, 2012 at 10:44
3 Answers 3
you should use jsonresult as action to send data back
public JsonResult details(string movieName)
{
var data = new {
name="Movie name"
};
return Json(data, JsonRequestBehavior.AllowGet);
}
answered Apr 11, 2012 at 10:48
1 Comment
cpoDesign
no you do not view.. this will return just an object with data.
public JsonResult Details(int id)
{
return Json(kr.GetMovie(id),JsonRequestBehavior.AllowGet));
}
As long as the Movie object is serializable this will work else you need to create a viewModel that will be a representation of your Movie object
answered Apr 11, 2012 at 10:48
Comments
public JsonResult Details(int id)
{
var data = kr.GetMovie(id);
return Json(data, JsonRequestBehavior.AllowGet);
}
You might want to look at web api also.
answered Apr 11, 2012 at 10:49
Comments
lang-cs