Return data Json

2

I have this function to return the data in JSON, but I can not get it to 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 in MVC COre, how can I convert it to work?

    
asked by anonymous 28.05.2018 / 16:09

1 answer

1

In spite of the same name, the method Json(object data) in Microsoft.AspNeCore.Mvc.JsonResult , which does not implement the JsonRequestBehavior argument that was present System.Web.Mvc.JsonResult , it ended up needing to return Json when action access was performed through 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 });
}
    
28.05.2018 / 17:22