5

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)
    {
    }

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Can i pass this query string in post ajax method? how the url will look like? screenMode will be part of post parameters or in query string?
Better still, you could put the ScreenMode in your model, have a hiddenFor in the view, then if you want to change it's value in Javascript, you can simply update the value of the hidden input, then when all is done, just do $("#form").serialize();, that way your action method need only take the view model as a parameter :)
Yes you are correct. I now use hidden field and update with Javascript. Can you please add this Hidden Field information as part of your answer. So it will be easily visible and helpful for others too :)
@mattytommo - back on the stack ;)
@DarrenDavies Can't keep away ;). Looks like you never left? :D
5

Also it's possible to create a place holder and then replace it:

var url = '@Url.Action("GetOrderDetails", "Home", new { id = "js-id" })'
          .replace("js-id", encodeURIComponent(rowId)); 

1 Comment

I prefer this approach
0

If you use T4MVC and jQuery, you can call the ActionResult doing the following:

In the controller:

public ActionResult SaveRoleDetail(RoleModel viewModel, ScreenMode screenMode)
{
}

In the view:

    $.post("@Url.Action(MVC.Home.SaveRoleDetail())", { viewModel: param1, screenMode: param2) }, function (data) {

        //Do Work

    });

2 Comments

It will be helpful if you add a link for T4MVC and jQuery. So people can easily navigate from here. Thanks
Sorry, I missed the links. Both are available as Nuget Packages. T4MVc can be found here (t4mvc.codeplex.com) and jQuery here (jquery.com) Hope this helps.
0

Access your route values (perhaps in html.HiddenFor). Values from JavaScript and Build your URL without @Url.Action. Use the URL to post.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.