1
Hello, I have a method called GetData()
in my Homecontroller that returns a JSON, I would like to pass the data to my View, but it always comes empty, I’m starting as a developer, and I’m already a few days into it, I hope it was clear and that someone can help me...
[HttpPost]
public JsonResult GetData()
{
List<Dados> qry = new List<Dados>();
using (AGPEntities md = new AGPEntities())
{
qry = (from s in md.Painel_Grafico
select new Dados
{
id_admAtribuido = s.id,
admAtribuido = s.admAtribuido,
quantidade_admAtribuido = (int)s.quantidade_admAtribuido
}).ToList();
}
return Json(qry, JsonRequestBehavior.AllowGet);
}
I don’t know much about Javascript and Ajax, but what I need is to recover Getdata to manipulate it. The following is a possible description of what I need, just playing on the console, if you get this later I turn around.
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: '@Url.Action("GetData")',
data: //O que colocar aqui?
success: function (result) {
console.log(result);
},
error: function (result) {
console.log("erro");
}
});
});
</script>
That question here served me as the basis for the question, but it didn’t work for me:
How to call a controller method from Ajax using MVC5 in visual studio?
Thanks in advance...
[EDIT] When I put the Getdata code inside the Actionresult Index() I get the following return:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Debugging, or accessing directly to
GetData()
the listqry
receives some value and the results are displayed?– Leandro Angelo
So, I do not know how to directly access Getdata, I marked it in debug but it is not called, what I did was to put this method inside the Actionresult Index(), so when the page is loaded it shows the data of qry...
– Caio Vinícius
this action is in your
HomeController
?– Leandro Angelo
Yes, this in Homecontroller
– Caio Vinícius