Syntax error when using new paragraph

0

I have a Javascript function like this:

<script>
    $(document).ready(function () {
        if('@TempData["Message"]' != "") {
            $("#myModal").modal();
        }
    });
</script>

Its function is to open a modal in case the TempData comes different from null.

In the backend populo that TempData as follows:

res.Error = string.Join<string>(Environment.NewLine, errores);
TempData["Message"]  = res.Error; 

As you can see, for every mistake I add a new line.

When I debug the function with Chrome

I get the following error:

  

Uncaught SyntaxError: Invalid or unexpected token

JavaScript Output:

      <script>
        $(document).ready(function () {
            if ('Ocurrio un error al ejecutar servicio web de Bono, para el empleado90 - NOMBRE 90 AP PAT 90 AP MAT 90
Ocurrio un error al ejecutar servicio web de Bono, para el empleado2021 - NOMBRE 2021 AP PAT 2021 AP MAT 2021
Ocurrio un error al ejecutar servicio web de Bono, para el empleado1451 - NOMBRE 1451 AP PAT 1451 AP MAT 1451
Se ejecutó el envío Bonos, sin embargo ocurrieron errores. Revisar log de errores.' != '') {
       $("#myModal").modal();
     }
});
    </script>

But I can not find the syntax error

Now try changing the script to:

  $(document).ready(function () {
            if ('@Html.Raw(TempData["Message"])' != '') {
       $("#myModal").modal();
     }
});

But it did not work either. If the error is only one, it works correctly. So the error is when inserting a new line. Does anyone have any idea how to solve this problem? Greetings

    
asked by David 04.03.2018 в 21:58
source

1 answer

0

The error effectively gives you the line breaks.

To avoid this you can use the JavaScriptStringEncode method of the class HttpUtility :

if ('@HttpUtility.JavaScriptStringEncode((string)TempData["Message"])'){
  ...
    
answered by 04.03.2018 / 22:30
source