0

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?

5
  • It's not clear what you're trying to accomplish. Is this happening server-side or client-side? (model sounds 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? Commented Jan 7, 2013 at 21:11
  • When my view loads, I want to run a javascript function that takes arguments from the model of the view. Commented Jan 7, 2013 at 21:14
  • It a a function defined in a js file. The function uses other functions in this same js file Commented Jan 7, 2013 at 21:27
  • Do you have a reference to that file? Commented Jan 7, 2013 at 21:42
  • Yes. other parts of the view are using it Commented Jan 7, 2013 at 21:43

4 Answers 4

5

well you can simply add the javascript in between the braces. So if you are using Razor

@if(model.count()>0){
    <script>
        // Javascript code here
    </script>
}

What this will do is add the javascript in the page which will then be executed when the browser renders the page.

A good practice would be to execute this code when the whole document is ready. If you are using jQuery, you can do this using $(document).ready(function(){ /*Code goes here*/ });

Sign up to request clarification or add additional context in comments.

7 Comments

Yes this is because you possibly define myFunction() after this call, or you haven't defined it at all!
It is defined in a javascript file while the above code is in the view
ok, but can you say for sure that the function is being executed by the browser before this one? It may appear before this code, but may actually be executed after this one... There isn't enough code for me to check it for you!
all other function within $(document).ready(function () {} work but for the one enclosed in the test condition that uses the model.
ok, try this to understand what I think is the problem: put all the code from myFunction() in place of where you are calling it. if that works I'll tell you more!
|
1

Try putting the if statement outside of the script tag like this

@if (Model.Count() > 0)
{
    <script type="text/javascript">

            $(document).ready(function () {
            myfunction(@Model.parameter);
         });

    </script>
}

2 Comments

still not getting my function. Is there an alternative way. My function basically inserts some html controls depending on the items in the model. What is the best way of doing this? 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>
@jpo Sorry, I can't provide any further help based on what's given. I tested the script above and it worked as expected. You can try writing your javascript in the view itself to eliminate the issue of not being able to reach the external file.
1

With JavaScriptModel ( http://jsm.codeplex.com ) you could write the following code in your controller action:

if (list.Count > 0)
{
    this.AddJavaScriptFunction("myjavascriptfunction", parameter);
}

You would not need to modify your view and you won't have any inline obstrusive javascript.

Comments

0

You could use a non-rendering html element (e.g a hidden Div) and write out the value of model.count, or even true/false to signify whether model.count > 0 into that hidden element. Then in your JavaScript you can obtain the value from the hidden element and run the condition.

Obviously this isn't appropriate if there are security implications with applying the condition. If this is the case then 'bPratik's suggestion would be more appropriate.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.