Doubts about validation for the implementation of the modal

Asked

Viewed 48 times

1

Hello, I’m making a control system and ended up locking in one part, I have two classes, the equipamento and the area, where an area may have one or many equipment and an equipment belongs to a single area...

I’m managing to do the crud of the right equipment, however the area I’m having difficulties, because as the equipment has the foreign key of the area, I can not exclude a certain area if some equipment is registered in it.

Soon to solve this problem I put a modal for when the user is going to delete an area with registered equipment, let him know that he must first edit the area of the equipment. If there is no registered equipment, you can confirm the exclusion!

Only I’m doubting what to put as a return in mine controller, and then move on to the View and open the modal.

I tried the following:

my repository:

//Este método verifica se há algum equipamento cadastrado na área
        public bool BuscarEquipamentoArea(string area)
        {
            strQuery = "select areArea from tblArea inner join tblEquipamento on areArea = equArea where areArea = @equipamento";
            return db.Database.SqlQuery<string>(strQuery, new SqlParameter("equipamento", area)).FirstOrDefault() != null ? true : false;
        }

mine AreaController:

 public ActionResult Delete(string id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblArea tblArea = db.tblArea.Find(id);
            if (tblArea == null)
            {
                return HttpNotFound();
            }
            AreaRepositorio areaRepo = new AreaRepositorio(db);
            if (areaRepo.BuscarEquipamentoArea(id) == true)
            {
                //não sei que tipo de retorno coloco aqui para avisar a view que tem q exibir o Modal
            }

            return View(tblArea);
        }

        // POST: Area/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {

                tblArea tblArea = db.tblArea.Find(id);
                db.tblArea.Remove(tblArea);
                db.SaveChanges();
                return RedirectToAction("Index");
        }

My View:

<td class="am">
      @Html.ActionLink("Editar", "Edit", new { id = item.areArea }) |
      @Html.ActionLink("Detalhes", "Details", new { id = item.areArea }) |
  @*(caso exista algum equipamento cadastrado nessa área, terá que ser igual a true)*@
      @if(algumaCoisaAqui== true){
          @Html.ActionLink("Delete", "Delete", new { data_toggle = "modal", data_target = "#btnDeletar" })
      }
      else{
           @Html.ActionLink("Excluir", "Delete", new { id = item.areArea })
      }
</td>

My modal:

<div class="modal fade" id="btnDeletar" role="dialog" data-backdrop="static">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dissmiss="modal">&times;</button>
                <h4 class="modal-title">Alerta!</h4>
            </div>
            <div class="modal-body">
                <p>Exclua ou Edite primeiro os Equipamentos cadastrados nesta área!</p>
            </div>
            <div class="modal-footer">
                <a href="~/" class="btn btn-default">Ir para Equipamentos</a>
                <button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
            </div>
        </div>
    </div>
</div>

I want to perform this check on the index, because before the user confirms the exclusion I already want to inform whether or not with the modal.

someone could help me to do this? I don’t know what to put in the commented fields

  • Just by the title I no longer understand your question... why the Controller should know anything that goes on in View?

  • 1

    So I don’t know how to do the validation right, like what would I put in the controller if there was a registered device? As I would warn View?

1 answer

2


Do so:

if(validacao)
{
      ... // O que tem que ser feito caso validação esteja certa.
      return RedirectToAction("VIEW_VALIDACAO_OK");
}
else 
{
      ViewBag.Erro = "MENSAGEM PARA A VIEW";
      return View("VIEW_QUE_ESTA",objeto)  
}

@{
    var varViewBag = Convert.ToString(ViewBag.Erro);
}


@if(varViewBag != null)
{
    <div class="alert alert-warning"> @varViewBag </div>
}

Browser other questions tagged

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