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.
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>