Modal de Confirmação

Asked

Viewed 1,120 times

3

I need to create a modal with a warning for the user when they click the save button. I already have a similar one that is used for when the user clicks on 'Delete' that is in my view /Delete

<h4 class="modal-title">Atenção!</h4>
<div class="modal-body">
Tem certeza que deseja excluir <strong>@Html.DisplayFor(model => model.Nome) </strong>?
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()    
<div class="modal-footer">
    <input class="btn btn-primary btn-danger" type="submit" value="Excluir" />
    <a class="btn btn-default" onclick="FechaModal();">Cancelar</a>
</div>
}

Controller

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Curso curso = db.Cursos.Find(id);
        db.Cursos.Remove(curso);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

I created a similar model called 'Aware' which is what I want to open when the user clicks to save

<div class="modal-body">
Apresente pessoalmente o comprovante de: @Html.DisplayFor(model => model.Nome)
</div>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()    
<div class="modal-footer">
     <input class="btn btn-primary" type="submit" value="Salvar" />
    <a class="btn btn-default" onclick="FechaModal();">Cancelar</a>
</div>

Cursocontroller

    [HttpPost, ActionName("Ciente")]
    [ValidateAntiForgeryToken]
    public ActionResult Createconfirmed(Curso curso)
    {
        if (ModelState.IsValid)
        {
            curso.AtualizaDiploma();
            db.Cursos.Add(curso);
            db.SaveChanges();
            return RedirectToAction("Index", "Perfis");
        }
    }

My Course Editor Templates view is below. When you click on Save from this view, the 'Aware' modal should appear for confirmation

@model Competências.Models.Curso

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-  data" }))

@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.PerfilId) 

<div class="modal-body">
        <div class="col-md-12">
            @Html.LabelFor(model => model.Nome)
            @Html.TextBoxFor(model => model.Nome, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Nome)
        </div>
    </div>

    <input class="btn btn-primary" type="submit" value="Salvar" />
    <a class="btn btn-default" onclick="FechaModal();">Cancelar</a>

My view Course/Create

@model Competências.Models.Curso
<div class="modal-header">
<h4 class="modal-title">Novo Curso/Formação</h4>
</div>
@Html.EditorForModel()

and my Editortemplate/Course

I already created a controller called Aware but then the modal appears with the information but not saved. Can anyone tell me where the mistake is and how to make this confirmation the right way?

1 answer

3

It’s missing inside your form the Id of the record to be deleted. Modify your View to the following:

<div class="modal-body">
    Apresente pessoalmente o comprovante de: @Html.DisplayFor(model => model.Nome)
</div>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()    
    @Html.Hidden("id", Model.CursoId)

    <div class="modal-footer">
        <input class="btn btn-primary" type="submit" value="Salvar" />
        <a class="btn btn-default" onclick="FechaModal();">Cancelar</a>
    </div>
}

Like the modal view code, the change is quite similar.


EDIT

For the case of creation, you need to put the fields as a form somehow. My suggestion is the following:

<div class="modal-body">
    Apresente pessoalmente o comprovante de: @Html.DisplayFor(model => model.Nome)
</div>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()    
    @Html.HiddenFor(model => model.CursoId)
    @Html.HiddenFor(model => model.Nome)
    @Html.HiddenFor(model => model.DataInicio)
    @* Coloque os outros campos aqui *@

    <div>Nome: @Model.Nome</div>
    <div>Data de Início: @Model.DataInicio</div>
    <!-- Mostre os demais campos aqui -->

    <div class="modal-footer">
        <input class="btn btn-primary" type="submit" value="Salvar" />
        <a class="btn btn-default" onclick="FechaModal();">Cancelar</a>
    </div>
}
  • But deleting is deleting and appearing the modal correctly. I want something similar at saving time as well. I edited my answer with the modal I created and the controller. Can you see please @Cigano Morrison Mendez ? Gratefully

  • 1

    @Jhenriquen I edited the answer.

  • This view you passed as an example is very similar to my Create view which is the modal that opens to fill in the form. Does the Aware view have to be similar? The controller I passed on the question is right?

  • 1

    That’s right, but the difference is that the fields in the modal are not editable. Controller is correct.

  • I edited my question by passing the Create controller which is what should call the modal Ciente @Cigano Morrison Mendez

  • 1

    It’s a little fuzzy. What’s the View Create and which is the View Aware? One of the Views does not open tag of form, then won’t save because without Html.BeginForm() MVC has no way of knowing which elements will be used for the Bind of data.

  • I added the fields, but when I click save the form is clean, not saved and no Confirmation Modal appears

  • I edited the question again @Gypsy Morrison Mendez, see if you can understand. Otherwise I will reformulate all of it again. Thank you for your patience.

  • 1

    @So Jhenriquen is still missing Html.BeginForm() in your View Ciente. Without it, the data will arrive empty in the Controller.

  • There is no missing @Gypsy Morrison Mendez, it is pq time to copy the code here I left behind. Until now updated on the question. My Course template editor has @Html.Beginform() and Aware Modal is, but it’s still not working

  • 1

    Try putting a breakpoint inside Createconfirmed, check the ModelState, if IsValid is true and also check the object curso. For what you have filled, they will come filled only PerfilId and Nome. Confers?

  • No, there’s more to fill in, I just summed up the code.

  • This action on my contrller called Createconfirmed was Create before it was working. I switched Actionresult from Create to Createconfirmed and added Actionname("Aware") to Httppost. I think my mistake is there. I should keep the Create controller as it was and add another Createconfirmed?

  • Well, without the full code it’s hard to help you.

  • I did what you said, I even took the other fields that I didn’t put here, and when I click Save my confirmation modal does not appear, it falls in the same form, only with a shape. There is a difference. My delete button is not on a modal. It is on a fixed page. When I click on it it opens a confirmation modal. In the home of the Course fill-in form, it is a modal and to click the save button would have to appear a new modal, ie modal on top of modal. That may?

  • @Jhenriquen If turned to the same form, it means that the Action did not pass the condition if (ModelState.IsValid), IE, something is with validation error. Put a breakpoint on this line and check within ModelState which variable has not been validated.

Show 11 more comments

Browser other questions tagged

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