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?
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
– JHenriqueN
@Jhenriquen I edited the answer.
– Leonel Sanches da Silva
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?
– JHenriqueN
That’s right, but the difference is that the fields in the modal are not editable. Controller is correct.
– Leonel Sanches da Silva
I edited my question by passing the Create controller which is what should call the modal Ciente @Cigano Morrison Mendez
– JHenriqueN
It’s a little fuzzy. What’s the View Create and which is the View Aware? One of the Views does not open
tag
ofform
, then won’t save because withoutHtml.BeginForm()
MVC has no way of knowing which elements will be used for the Bind of data.– Leonel Sanches da Silva
I added the fields, but when I click save the form is clean, not saved and no Confirmation Modal appears
– JHenriqueN
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.
– JHenriqueN
@So Jhenriquen is still missing
Html.BeginForm()
in your ViewCiente
. Without it, the data will arrive empty in the Controller.– Leonel Sanches da Silva
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
– JHenriqueN
Try putting a breakpoint inside
Createconfirmed
, check theModelState
, ifIsValid
istrue
and also check the objectcurso
. For what you have filled, they will come filled onlyPerfilId
andNome
. Confers?– Leonel Sanches da Silva
No, there’s more to fill in, I just summed up the code.
– JHenriqueN
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?
– JHenriqueN
Well, without the full code it’s hard to help you.
– Leonel Sanches da Silva
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
@Jhenriquen If turned to the same form, it means that the
Action
did not pass the conditionif (ModelState.IsValid)
, IE, something is with validation error. Put a breakpoint on this line and check withinModelState
which variable has not been validated.– Leonel Sanches da Silva