Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
Calling window.onload inside a cshtml view

I am trying to call a window.onload function inside a cshtml view.

My code looks like this.

@model BusinessModel
    @if(Model.Business != null)
    {
        <script>
        window.onload = function () {
            if ('@Model.Business'!='') {
                var data = {
                //JSON Object
                };
                
                $.ajax({
                url: www.google.com,
                method: 'POST',
                headers:{},
                data : JSON.stringify(data),
                success: function (response){
                window.location.replace(new url);
                }
                
                });
                
                return false;
            }
        }
</script>
    } else {
         // Some basic HTML code
    }

When i am trying to do something like this, when the condition matches it is never hitting the window.onload function.

Am i doing something wrong or Does this need to be handled in a different way? Please help me with this

Thanks.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try to check if Model.Business is null in js.Here is a demo:

Model:

public class BusinessModel
    {
        public List<Business> Business { get; set; }
       
    }

Action:

public IActionResult Test1()
        {
            var model = new BusinessModel { Business = new List<Business>() };
            return View(model);

        }

View:

@model BusinessModel
@if (Model.Business == null)
{
    <h1>null</h1>
    // Some basic HTML code
}
<script>

        window.onload = function () {
            if ('@Model.Business'!='') {
                alert("Hello");
            }
        }

    </script>

result: enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda