Aaron Bar
Aaron Bar

Reputation: 620

JQuery variable does not exist in the current context

I am attempting to call another JQuery fuction from this one and the alert is able to use the variables reviewMessage and payItemIDValue but I receive an error on the refreshView line of code:

The name 'payItemIDValue' does not exist in the current context

Why won't it recognize the variable name?

    $(document).ready(function () {
        $('#submitReview').click(function () {
            var reviewMessage = $('#message-text').val();
            var payItemIDValue = $('#payItemID').val();
            alert('PayItemID = ' + payItemIDValue + 'Message: ' + reviewMessage);
            refreshView('@Url.Action("RequestPayItemReview", "Employee", new { payItemID = payItemIDValue, message = "" })', '#employeeEditTabPanel')
        });
    });

Upvotes: 1

Views: 522

Answers (1)

trincot
trincot

Reputation: 350667

I assume you are using Razor. @Url.Action is server-side code, which is executed before any of the JavaScript code is executed. So you cannot reference JS variables in that @Url.Action() call. You'll have to append the JS value to it:

refreshView(`@Url.Action("RequestPayItemReview", "Employee")?payItemID='${payItemIDValue}&message=`, '#employeeEditTabPanel')

Upvotes: 1

Related Questions