Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!