Edit using PUT method

Asked

Viewed 22 times

0

I wanted to make an edition by PUT method, but I have some problem with the query string. I could not solve so far because I am a new programmer in . NET Core. I have tried to define route, use Frombory, but so far without success. This is my view:

@model Teste_Tria_Software.Models.Cliente

@{
    ViewData["Title"] = "EditPut";
}

<h4>Cliente</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="put" asp-controller="Clientes" asp-action="EditPutCliente">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="ClienteID" />
            <div class="form-group">
                <label asp-for="CPF" class="control-label"></label>
                <input asp-for="CPF" class="form-control" />
                <span asp-validation-for="CPF" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="NOME" class="control-label"></label>
                <input asp-for="NOME" class="form-control" />
                <span asp-validation-for="NOME" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="EMAIL" class="control-label"></label>
                <input asp-for="EMAIL" class="form-control" />
                <span asp-validation-for="EMAIL" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="DATA_CRIACAO" class="control-label"></label>
                <input asp-for="DATA_CRIACAO" class="form-control" />
                <span asp-validation-for="DATA_CRIACAO" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

This is my controller:

        [HttpPut("Clientes/EditPutCliente/{id}")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> EditPutCliente(long id, [FromBody] Cliente cliente)
        {
            if (id != cliente.ClienteID)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(cliente);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ClienteExists(cliente.ClienteID))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(cliente);
        }

That is the mistake:

Could not find this site pageNo web page was found for the web address:https://localhost:44313/Clients/Editputclient? Clienteid=1&CPF=99999997&NAME=José+da+Silva+Rodrigues&email=Jose%40outlook.com.br&DATA_CREATION=2021-06-30T10%3A59%3A00.000&__Requestverificationtoken=Cfdj8ljgpzwdgpxiipjnonuamp5-7DShQE9W3B9KEw_O8rMVaLSTXIg-Suofk1i0nqzwumzlxunploo7gfdspbi3szara3wglqmpdprrkzqbmkwrorep4rrk88u39nzljwl2gry_jttpzmoofnvjwlh6uqw HTTP ERROR 404

  • 2 things wrong. 1: Customer id needs to be part of the route, not querystring. 2. A PUT request cannot be made with an HTML form, GET or POST only.

  • How can I use the PUT method by Viewmodel?

No answers

Browser other questions tagged

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