When I delete the model name is not shown

Asked

Viewed 33 times

0

I did an action to delete and delete, but View delete does not show 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 as [Httppost]. Is that normal? See in this screenshot what I’m saying inserir a descrição da imagem aqui

See that Property name is empty and should appear Los Angeles

EDIT1

I did that and it’s still the same

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

            var model = await cidade.GetCidades();

            return View();
        }

        // POST: Cidade/Delete/5
        [HttpPost]
        public async Task<ActionResult> Delete(int id, Cidade cidade)
        {
            try
            {
                DeleteCidadeAsync deleteCidade = new DeleteCidadeAsync();

                await deleteCidade.DeleteCidade(id, cidade);

                return RedirectToAction("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 this way and the previous, both are deleting. Just does not show the name of the guy to be deleted.

  • Your problem is that Displayfor is not displaying the value of the model name property?

  • @Netinhosantos, that wouldn’t show(model => model.nome)?

  • Note that your get action is not returning your model. Return View(model);

  • @Netinhosantos, if I play model gives error. I will catch the error again and post

1 answer

1

You see, you are deleting the city already, and if you want to show some data from that city, you need to have two Delete methods, one GET (to show the city information) and one 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.

  • lacked the ActionName, so it will give error, two methods, same name and both wait 1 int

  • 1

    Oops! True dude, I wrote right here and I didn’t even notice. Valeuzão!

Browser other questions tagged

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