MVC5 + Entity Framework + Form + Modal

Asked

Viewed 102 times

0

I have an application where there is a central entity and several other auxiliary registers, such as Status, Format, Type, etc. All these auxiliary entities have only 2 fields: Name and Status (boolean, stating whether it is active or not). Since they are very simple entities, I would not like to create a screen for each one. In addition, I would like to allow the user, when registering a record of the main entity, to do maintenance in these auxiliary entities (exclusion, inclusion, editing...).

However, when I work with Partialviews and forms, I have a problem, because the screen is loaded again, for validations in the backend and, when it is returned, the screen is reloaded, losing the state of open modals and everything else.

How to do this type of maintenance with modals? I would like to open 2 modals: one for listing and one for editing/insertion.

Form field with Format CRUD button Campo do formulário com botão para CRUD de Formatos

Format Listing Listagem de Formatos

Form with editing/insertion of Formats Formulário com edição/inserção de Formatos

When I click the Create button, in the form inside the modal, the screen is redirected and I miss all the state of the main form and open modals.

  • Inlua the code of your views and controller only with the image can not answer. But in the backend validation you need to popular these components indicating which value had been selected.

  • Basically, they are partialviews (for auxiliary forms) within modals, in a main form (View). It’s not much of a secret, and I’m starting the code now. I did just one test, adding an error in Modelstate and when the validation returns the result, the screen is redirected to the partialview action.

  • I know, but the code is more useful than the images.

1 answer

0

public PartialViewResult Editar(int id)
{
    var context = new Entities();
    int codEps = GetUsuarioLogado().CodEmpresa;

    if (id == 0) return PartialView(new FORMATO()
    {
        ATIVO = false
    });

    var formato = context.FORMATO.Where(f => f.COD_EPS == codEps && f.COD_FORMATO == id).FirstOrDefault();

    return PartialView(formato != null ? formato : new FORMATO()
    {
        ATIVO = false
    });
}

[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult Editar(FORMATO formato)
{
    ModelState.AddModelError("NOME", "Erro");
    return PartialView(formato);
}

The JS code to open the editing modal is this:

function CarregarModalFormatoEdit(id) {
    $.ajax({
        url: '@Url.Action("Editar", "Formatos", null)',
        data: { id },
        success: function (response) {
            $('#modalEditWrapper').html(response);
            $('#modalFormatoEdit').modal('show');
        }
    });
}

The idea of JS is only popular the return of Partialview within the modal-body and display the same. Update, Insert and delete operations would be inside Partialview and Controller itself.

Browser other questions tagged

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