Javascript int variable from ASP.NET MVC Model data?

Posted by Anders Svensson on Stack Overflow See other posts from Stack Overflow or by Anders Svensson
Published on 2011-01-11T22:50:59Z Indexed on 2011/01/11 22:53 UTC
Read the original article Hit count: 155

I need to get model data into a javascript variable and use it as an int to compare values. But I can only figure out how to get the model data as strings, otherwise the compiler complains.

So how can I get the max and taskBudgetHours as int variables in the Javascript?

<script type="text/javascript">
    $(document).ready(function () {
        $("#taskForm").submit(function (e) {
            var taskBudgetHours = $('#BudgetHours').val();
            var max = '<%: Model.Project.RemainingBudgetHours %>';
            var test = 'test';
            alert(taskBudgetHours);
            alert(max);
            if (taskBudgetHours <= max) { //This doesn't work, seems to treat it as strings...
                return true;
            }
            else {
                //Prevent the submit event and remain on the screen
                e.preventDefault();
                alert('There are only ' + max + ' hours left of the project hours.');
                return false;
            }
        });

        return;
    });
</script>

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about jQuery