Knowing ModelState in javascript dynamically

0

I'm developing an application in ASP.NET MVC 5 and I have a problem in putting a modal load while the form is saved, because when there is something invalid in the form the javascript does not respect, showing the loading mode, I would like know how to get the Dynamic ModelState to release the modal only when it is true, thus avoiding showing the modal wrongly.

I'm trying to use it this way without success:

$('#salvar').click(function () {
            var isValid = ${ViewData.ModelState.IsValid};
            if (!isValid) {
                waitingDialog.hide();
            } else {
                waitingDialog.show('Salvando Chamado...');
            }
        });
    
asked by anonymous 06.11.2015 / 19:40

1 answer

0

First you have to understand that the code impression of your View ( Html , css , javascript ...) and code rendering Razor a View Engine in ASP.NET ) is done the moment the request of your page is accepted by the server, thereby causing nothing else coded in Razor to be interpreted again in that View .

When performing any request Ajax trying to pass a value rendered by Razor has already been printed there at the beginning of code rendering.

Follow the example:

Code html with Razor :

@{
  ViewBag.StringTeste = "Renderizou!";
}

<script>
  function ImprimeValorRazor() {
      var stringExemple = '@ViewBag.StringTeste';
      alert(stringExemple);
  };
</script>

Rendered Code:

<script>
  function ImprimeValorRazor() {
      var stringExemple = 'Renderizou!';
      alert(stringExemple);
  };
</script>

This control of ModelState is better controlled by your application and not by your View, because if the user changes the code through the browser it will be able to cause exceptions directly in your application, even bypassing their validations.

Make it display a message to the user with the validations that did not happen on the return of your request or use the Data Notations directly in the entity for automatic validation.

    
14.01.2019 / 17:03