Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8422461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:27:10+00:00 2026-06-10T03:27:10+00:00

I know there are already a few posts on this subject but I cannot

  • 0

I know there are already a few posts on this subject but I cannot seem to figure out an answer to my specific issue. I am confused as to why I cannot pass a variable into the controller but I can pass in a hardcoded value…

This works…

<script type="text/javascript">
$().ready(function () {
$("#MessageTypes").change(function () {
        //I know this is a horrible way to do it but for some reason I couldnt pass the sMessageType directly in
        var sMessageType = $("#MessageTypes").val();
        if (sMessageType == "Professional Voicefile") {
            $.get('@Url.Action("GenerateMessageDesc", new { MessageType = "Professional Voicefile" } )', function (data) {
                $('#MessageDesc').replaceWith(data);
            });
        }
        else if (sMessageType == "Dynamic Field") {
            $.get('@Url.Action("GenerateMessageDesc", new { MessageType = "Dynamic Field" } )', function (data) {
                $('#MessageDesc').replaceWith(data);
            });
        }
        else {
            //default to prof
            $.get('@Url.Action("GenerateMessageDesc", new { MessageType = "Professional Voicefile" } )', function (data) {
                $('#MessageDesc').replaceWith(data);
            });
        }
    });
}); 

…But this does not. Why?

<script type="text/javascript">
$().ready(function () {
    $("#MessageTypes").change(function () {
        var sMessageType = $("#MessageTypes").val();
            $.get('@Url.Action("GenerateMessageDesc", new { MessageType = sMessageType } )', function (data) {
                $('#MessageDesc').replaceWith(data);
            });
        }
    });
}); 

It says “The name ‘sMessageType’ does not exist in the current context”.

I think maybe I should be using some kind of ajax call instead to call the controller and update the view instead of how I am doing it — however, why does scenario 1 work but not scenario 2?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:27:11+00:00Added an answer on June 10, 2026 at 3:27 am

    It says “The name ‘sMessageType’ does not exist in the current context”.

    sMessageType is a javascript variable that lives on the client that you are trying to use inside a server side helper. That obviously is impossible because javascript runs on the client and server side script on the server.

    Here’s the correct way to achieve that:

    $.get('@Url.Action("GenerateMessageDesc")', { messageType: sMessageType }, function (data) {
        $('#MessageDesc').replaceWith(data);
    });
    

    this will pass the MessageType as query string parameter, so your target controller action might look like this:

    public ActionResult GenerateMessageDesc(string messageType)
    {
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are already some posts about this topic, but I cannot find
I know there are already a few questions like this but I can't seem
Hey I know there are already a few posts about this - yet I
I know there are already some questions about this, but not that specific. I
I know there are a few questions on stack already regarding this, and I
I know there are already many questions like mine but I found no answer
There's already a question on this here. But i want to know if its
I know there's already a lot of questions on this topic but I might
I know there are quite a few questions on this already and i've looked
I know there's quite a few topics on this already, and I've read quite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.