Doubt Json MVC Core Razor

Asked

Viewed 49 times

1

I have created a method to perform a Insert through Ajax, thus:

function SalvarHorario() 
{

    //NomeHorario
    var nome = $("#Nome").val();

    var token = $('input[name="__RequestVerificationToken"]').val();
    var tokenadr = 
        $('form[action="/Horario/Create"] input[name="__RequestVerificationToken"]').val();
    var headers = {};
    var headersadr = {};
    headers['__RequestVerificationToken'] = token;
    headersadr['__RequestVerificationToken'] = tokenadr;

    //Gravar
    var url = "/Horario/Create";

    $.ajax({
        url: url
        , type: 'POST'
        , headers: headersadr
        , data: { Id: 0, Nome: nome, __RequestVerificationToken: token }
        , success: function (data) {
            //if (data.Resultado > 0) {
            ListarItens(data.Resultado);
            //}
        }
    });
}

Except that it does not recognize the CREATE method that I call in Action, the page that I’m taking the data, was created by Razor CRUD. It only includes through the automatic class that is created:

public async Task<IActionResult> OnPostAsync()
{
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Horarios.Add(Horarios);
            await _context.SaveChangesAsync();
            return RedirectToPage("./Index");
}

I created a controller, and I need it to search the method of this controller, I tried to change its name, and nothing certain, follows the method I need to call:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Horarios h)
{
     if (ModelState.IsValid)
     {
           _context.Horarios.Add(h);
           _context.SaveChanges();
     }
     return Json(new { Resultado = h.Id });
 }

What am I doing wrong? If I take this code public async Task<IActionResult> OnPostAsync() it returns me error of Bad Request 400.

I’ve tried several ways none works, I’m learning MVC Core Entity Framework now, so I’m still catching a lot.

  • You are using Razor Pages?

  • I am!! It’s like he doesn’t recognize the create function!! I’m still new with mvc ..

  • Razor Pages is different from MVC Controller !!! You will need to choose the right project, because MVC has a different behavior than Razor Pages.

  • Do you have an example that can help me ? The strange thing is that he enters the function, and soon after has 3 more functions, and he performs them, because only this create q he n recognizes ? The difference between create and other is the passage from id to json function..

  • https://docs.microsoft.com/pt-br/aspnet/core/mvc/razor-pages/? view=aspnetcore-2.0&tabs=visual-studio e https://docs.microsoft.com/pt-br/aspnet/core/mvc/razor-pages/razor-pages-conventions?view=aspnetcore-2.0. these two links may help you, but remembering that Razor Page works with convention and MVC does not, if mixed in one another that was the problem, the create method will not work even ...!! (thus)

  • Virgilio checking here, he enters the function, I put an Alert after the sucess, and he appears normal, it just does not include, apparently I did not find any problem with the function.

  • If you have to debug the code serve side! has already done this?

  • It only includes if you have the entityframework include code the other functions work, which are called right after this, how can I get the id then ? If I get the ID on include, it solves my problem.

  • has to see the whole code...

  • public async Task<Iactionresult> Onpostasync() this function that includes, works, and enters Salvarhorario, I put an Alert, only I need it to get the ID of the last saved time.

  • 1

    it should be like this: public IActionResult OnPostResult()&#xA;{&#xA; return new JsonResult(new { Resultado = Horarios.Id });&#xA;}

  • 1

    Thanks, it worked.

Show 7 more comments
No answers

Browser other questions tagged

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