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
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?
– Netinho Santos
@Netinhosantos, that wouldn’t show(
model => model.nome
)?– pnet
Note that your get action is not returning your model. Return View(model);
– Netinho Santos
@Netinhosantos, if I play model gives error. I will catch the error again and post
– pnet