MVC5 + Entity Framework + Form + Modal

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, informing if 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, by registering a registry of the principal entity, to perform maintenance on these auxiliary entities (exclusion, inclusion, editing ...).

However, when I work with PartialViews and forms, I have a problem, because the screen loads again, for backend validations, and when it is returned, the screen is reloaded, losing the state of open manners and everything else. / p>

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

Form Field with Button for Format CRUD

FormListing

Formwithediting/insertionofFormats

When I click the Create button, on the form within the modal, the screen is redirected and I lose all the state of the main form and modal open.

    
asked by anonymous 05.02.2018 / 22:57

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 mode 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 to return the PartialView inside the modal-body and display the same. The update, insert and delete operations would be inside the PartialView and Controller itself.

    
06.02.2018 / 15:23