When deleting the name in the model is not shown

0

I made an action to delete and delete, but in View delete does not appear the value of the field, which comes from the model. This is cshtml:

@model TreinamentoCrud.Models.Cidade

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Cidade</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.nome)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.nome)
        </dd>

    </dl>

    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()

        <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-default" /> |
            @Html.ActionLink("Voltar", "Index")
        </div>
    }
</div>

and this is the controller

public async Task<ActionResult> Delete(int id)
        {
            DeleteCidadeAsync deleteCidade = new DeleteCidadeAsync();

            await deleteCidade.DeleteCidade(id);

            return View();
        }

However delete, the attribute in the method is like [HttpPost] . Is this normal? See in this screenshot what I am saying

SeethattheNamepropertyisemptyandshouldappearLosAngeles

EDIT1

Ididthatanditcontinuesthesamething

[HttpGet]publicasyncTask<ActionResult>Delete(intid){GetCidadesAsynccidade=newGetCidadesAsync();varmodel=awaitcidade.GetCidades();returnView();}//POST:Cidade/Delete/5[HttpPost]publicasyncTask<ActionResult>Delete(intid,Cidadecidade){try{DeleteCidadeAsyncdeleteCidade=newDeleteCidadeAsync();awaitdeleteCidade.DeleteCidade(id,cidade);returnRedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

My method is like this now

public class DeleteCidadeAsync
    {
        HttpClient client = new HttpClient();

        public async Task DeleteCidade(int id, Cidade cidade)
        {
            string url = $"http://localhost:56137/api/DeleteCidade/{id}";
            await client.DeleteAsync(url);
        }
    }

See that way and the previous one, both are deleting. It just does not show the name of the face to be deleted.

    
asked by anonymous 09.08.2018 / 20:58

1 answer

1
Well, you're deleting the city already, and if you want to show some data for that city, you need to have two Delete methods, a GET (to display city information), and a POST (to confirm the deletion):

    [HttpGet]
    public async Task<ActionResult> Delete(int id)
    {
        SuaClasseDeBuscarCidadesbusca = new SuaClasseDeBuscarCidades();

        Cidade cidade = busca.SeuMetodoQueBuscaCidade(id);

        return View(cidade);
    }

    [HttpPost]
    [ActionName("Delete")]
    public async Task<ActionResult> ConfirmDelete(int id)
    {
        DeleteCidadeAsync deleteCidade = new DeleteCidadeAsync();

        await deleteCidade.DeleteCidade(id);

        return RedirectToView("Index");
    }

The post method should be called by clicking the Delete button, which confirms the removal of the city.

    
09.08.2018 / 21:11