Validate delete in controller, return html helper

2

I have my registration, which was done using Scallfold's visual studio In my view:

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

        <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-danger" /> |
        </div>
    }

And in my controller:

public ActionResult DeleteConfirmed(string id)
{
    Cliente cliente = db.Clientes.Find(id);
    db.Clientes.Remove(cliente);
    db.SaveChanges();
    return RedirectToAction("Index");
}

How do I validate the deletion if registration already exists with this client? How to return to View using html helper?

    
asked by anonymous 12.06.2014 / 02:30

1 answer

2

Place in View:

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

    <div class="form-actions no-color">
        <input type="submit" value="Delete" class="btn btn-danger" /> |
    </div>
}

Controller:

public ActionResult DeleteConfirmed(string id)
{
    Cliente cliente = db.Clientes.Include(c => c.Registros).SingleOrDefault(c => c.Id == id);
    if (cliente.Registros.Count > 0) {
        ModelState.AddModelError("", "Cliente possui registros pendentes.");
        return View("Delete", cliente)
    }

    db.Clientes.Remove(cliente);
    db.SaveChanges();
    return RedirectToAction("Index");
}
    
12.06.2014 / 02:37