Data return Json

Asked

Viewed 503 times

2

I have this function to return the data in JSON, but I’m not able to make it work in MVC Core.

 public ActionResult SalvarItens(string HoraInicio, string HoraFim, bool Seg, bool Ter, bool Qua, bool Qui, bool Sex, bool Sab, bool Dom, bool Fer, int Tipolimite, int Limiteacessos, int HorarioId)
        {
            var item = new HorariosItens()
            {
                HoraFim = HoraFim,
                HoraInicio = HoraInicio,
                Seg = Seg,
                Ter = Ter,
                Qua = Qua,
                Qui = Qui,
                Sex = Sex,
                Sab = Sab,
                Dom = Dom,
                Fer = Fer,
                Tipolimite = Tipolimite,
                Limiteacessos = Limiteacessos,
                HorarioId = HorarioId,
            };

            try
            {
                _context.HorariosItens.Add(item);
                _context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            //return Json(new { Resultado = item.Id }, JsonRequestBehavior.AllowGet);
        }

This return Json(new { Resultado = item.Id }, JsonRequestBehavior.AllowGet); does not work on MVC Core, how can I convert to work?

  • You don’t need the JsonRequestBehavior.AllowGet

  • I work with Mvccore, this option does not appear to me.

1 answer

1

Despite the same name the method Json(object data) in the Microsoft.AspNeCore.Mvc.JsonResult, that does not implement the argument of JsonRequestBehavior who was present System.Web.Mvc.JsonResult and eventually it was necessary to return the Json when access to the action was accomplished through a POST.

public ActionResult SalvarItens(string HoraInicio, string HoraFim, bool Seg, bool Ter, bool Qua, bool Qui, bool Sex, bool Sab, bool Dom, bool Fer, int Tipolimite, int Limiteacessos, int HorarioId)
{
    var item = new HorariosItens()
    {
        HoraFim = HoraFim,
        HoraInicio = HoraInicio,
        Seg = Seg,
        Ter = Ter,
        Qua = Qua,
        Qui = Qui,
        Sex = Sex,
        Sab = Sab,
        Dom = Dom,
        Fer = Fer,
        Tipolimite = Tipolimite,
        Limiteacessos = Limiteacessos,
        HorarioId = HorarioId,
    };

    try
    {
        _context.HorariosItens.Add(item);
        _context.SaveChanges();
    }
    catch (Exception ex)
    {
        throw ex;
    }

    return Json(new { Resultado = item.Id });
}
  • I can’t put Json, it doesn’t recognize, and it returns the error. json doesn’t exist in the current context.

  • which version of dotnet core? SalvarItens() is a ActionResult or a Task<IActionResult> ?

  • I was able to solve it by doing this: Return Content("new { Result = item. Id }");

  • This will return you a string, not your serialized object

  • But the way he helped me he doesn’t recognize the Json, There’s some reference I should make ?

  • What class is your controller inheriting? Ps.: This method is in a Controller, right?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.