Call a function when the view loads in an MVC application

Solution

Document.ready function is for this purpose. For example, you want to call a function that fills a drop-down dynamically when the view loads, you will give that function a call in document.ready function enclosed in the script tag

<script>
$(document).ready(function () {
    fillRoles();
});
</script>

In the above example, function fillRoles() is called to fill the Roles drop-down as soon as the form loads. Similarly, you can place all your code that needs to execute at this stage

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started