MVC - Keep Partialview loaded when Modelstate is not valid

Asked

Viewed 334 times

0

I have a simple registration screen that, when selected new record, is opened a modal (which is a partialview in the project) for the user to inform the fields for registration.

The view call is performed through Jquery:

$("#btn-Incluir").click(function () {
    $("#modalCadastro").modal({
        backdrop: 'static',
        keyboard: true
    }, 'show');
});

The save button of Partialview is the type submit, and inside my controller have the method below:

public virtual ActionResult Salvar(TViewModel model)
    {
        if (ModelState.IsValid)
        {
            if (model.Operacao.Equals("I"))
                Inserir(model);
            else
                Atualizar(model);                

            return RedirectToAction(NmView);

        }
        else
        {
            return PartialView(model);
        }
    }

The problem is that when the ModalState is invalid, error is displayed below:

The partial view 'Save' was not found or no view engine Supports the searched Locations. The following Locations Were searched

If I change and return and pass the path of mine partial view, the system displays only the partial view, no longer as modal daughter of the main page.

How do I make that when the ModalState is invalid, continue with modal open and only present errors on page via ValidationMessageFor.

Thank you!

  • There is a Partialview calling for Salvar.cshtml within the subdirectories corresponding to its Controller which is inside the directory Views?

  • No. Save is just a method within my controller, which returns a result action.

1 answer

2


There are a lot of bad practices here. Within your method Salvar, you have:

public virtual ActionResult Salvar(TViewModel model)
{
    if (ModelState.IsValid)
    {
        if (model.Operacao.Equals("I"))
            Inserir(model);
        else
            Atualizar(model);                

        return RedirectToAction(NmView); // Isto muda o estado atual da página

    }
    else // Desnecessário
    {
        return PartialView(model); // Isto não necessariamente muda o estado atual da página.
    }
}

Besides, you don’t have a Partial calling for Salvar.cshtml, which causes the error.

If the idea of the method Salvar is not modify the current status of the page, the method should be implemented as:

public virtual JsonResult Salvar(TViewModel model) { ... }

And the method should be called through Ajax.

If the idea is to actually modify the current state of the page, you can redirect to the same document, but opening the modal to display, for example, validation messages, is not guaranteed:

public virtual ActionResult Salvar(TViewModel model)
{
    if (ModelState.IsValid)
    {
        if (model.Operacao.Equals("I"))
            Inserir(model);
        else
            Atualizar(model);                

        return RedirectToAction(NmView);
    }

    return View(model); // Troque aqui.
}
  • Thanks for the tips. Actually when Modelstate is valid, Modal will be closed and the main page loaded. When an error occurs that the status of the page should be maintained and the modal loaded. In this scenario it is still more recommended to return Jsonresult?

  • Yes. You will have to do an Ajax that will wait for a JSON to return. If the return is a successful JSON, the status of the page changes. Otherwise, Javascript should send an error message.

Browser other questions tagged

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