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 9123633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:23:17+00:00 2026-06-17T06:23:17+00:00

I would like to pass a javascript variable in a @Url.Action method as a

  • 0

I would like to pass a javascript variable in a @Url.Action method as a route parameter.

I like to pass the screenmode javascript variable as a route parameter to my action method.

I have a view model with ScreenMode enum property and based on it i
should call a action in Ajax. I also need to pass a javascript
variable as a parameter for route.

This is what i tried and got compilation error.

The name ‘screenMode’ does not exist in the current context

  $("#role-detail-form").submit(function (e) {

    if ($(this).valid()) {
    var screenMode = 0;
    @{
     if (Model.ScreenMode == UI.ViewModel.ScreenMode.New)
     {
       <text> 
       screenMode =2; 
       </text>
     }
    }
    $.post('@Url.Action("SaveRoleDetail", new { mode=screenMode})', 
            $(this).serialize(), function (data) {
                $("#role-detail").html(data);
                $.validator.unobtrusive.parse($("#role-detail"));
            });
        }
        e.preventDefault();
    });

My Action is

 public ActionResult SaveRoleDetail(RoleModel viewModel, ScreenMode screenMode)
    {
    }
  • 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-17T06:23:18+00:00Added an answer on June 17, 2026 at 6:23 am

    You’d have to split that out and build the query string yourself, in order to incorporate the Javascript variable.

    Something like this is what you need:

    $.post('@(Url.Action("SaveRoleDetail"))?screenMode=' + screenMode)
    

    EDIT: Although probably best practice, you should store the ScreenMode variable in your Model, then put a HiddenFor in your view for it. Then, whenever you change the value in Javascript, simply update the value of the hidden input, that way your action method only needs to take the view model as a parameter. If you are posting the form in JavaScript and you can call $("#form").serialize() to send all the data back within your post call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to pass a nested JavaScript object to my ASP.NET MVC Action
i would like to pass a variable into my javascript, using simple modal plugin,
I would like to pass {id:1, name:stackoverflow, parameter2:false, parameter3:true} To my action public JsonResult
I would like to pass some attributes to actionListener method. My implementation is like...
I have a form that has label values that I would like to pass
I have a ratingbar in my app, and I would like when I pass
I would like to choose which variable to pass to a funciton based on
I would like to find out if a Javascript variable exists. This is what
Possible Duplicate: How to pass value from javascript to php file would something like
I would like to create a String.replaceAll() method in JavaScript and think using a

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.