I have a view in which I do some condition check when the page load.
if(model.count()>0){
}
I then want to call a javascript function if this condition is satisfied. How can I do this please?
I know how to do that within an html control but can this be done without any control?
Or how can i do this check in the javascript, thus having to refer to the view's model?
EDIT
This is what I have now, but the function is not recognized: The name myfunction does not exist in the current context
<script>
$(document).ready(function () {
@if (Model.Count() > 0)
{
myfunction(Model.parameter);
}
});
</script>
EDIT
I changed that to
@if (Model.Count() > 0)
{
<script type="text/javascript">
$(document).ready(function () {
myfunction(@Model.parameter);
});
</script>
}
But not working. Even if I try something like alert("test" + @Model.parameter) within the .ready(function()), it does not work.
Alternatively, I will have to write the code in the view. I tried eliminating the function and doing it in the view but i will need a way of setting the id property of html element to a variable. something like var variable = Model.param1 and then having <div id=variable></div>. But how can I set the id property to a variable?
modelsounds like a server-side reference in this case.) You don't need any HTML elements in order to run JavaScript code, I'm not really sure why you'd think that. What check are you trying to perform in JavaScript?