Sax*_*man 7 c# asp.net-mvc asp.net-mvc-3
我可以从ActionResult调用JsonResult方法吗?我想要做的是在我的MVC.Site项目中有一个专门用于处理API的区域(只返回json以便我可以重用非mvc项目).然后从另一个ActionResult(我处理数据和视图),我想调用JsonResult,然后返回Json数据和View信息.即:
public JsonResult GetSongs()
{
var songs = _music.GetSongs(0, 3);
return Json(new { songs = songs }, JsonRequestBehavior.AllowGet);
}
public ActionResult Songs()
{
// Get the data by calling the JsonResult method
var data = GetSongs();
return Json(new
{
// Render the partial view + data as json
PartialViewHtml = RenderPartialViewToString("MyView", data),
success = true
});
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
是的,这是完全可以接受的.
全部Results继承自ActionResult.有关课程的详细信息,请查看本文ActionResult.
public JsonResult GetSongs()
{
var songs = _music.GetSongs(0, 3);
return Json(new { songs = songs }, JsonRequestBehavior.AllowGet);
}
public ActionResult GetSongs()
{
var result = GetSongs();
return Json(new
{
// The JsonResult contains additional route data and view data.
// Your view is most likely interested in the Data prop (new { songs = songs })
// depending on how RenderPartialViewToString is written you could also pass ViewData
PartialViewHtml = RenderPartialViewToString("MyView", result.Data),
success = true
}, JsonRequestBehavior.AllowGet);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12470 次 |
| 最近记录: |