3

How can I pass my client side variable to controller using Url.Action.

I am trying to do like this, but it's not working

location.href = '@Url.Action("printtopdf","batch",new {allocationId=unSelected}))';

In the above line 'unSelected' is a client side variable and comma separated values.

2 Answers 2

5

I think this will work for you. I find it a bit more cleaner and is an alternative option.

window.location = "/batch/printtopdf?allocationId=" + unSelected;

It stands for

window.location = "/yourControllerName/yourActionMethodName?QueryStringId=" + yourVariable;
Sign up to request clarification or add additional context in comments.

1 Comment

unSelected is an comma seperated values. Shall we able to send it as you mentioned?
4

@Url.Action creates the link on the server, but unSelected only exists on the client. What you would need to do is append the unSelected querystring to the generated link.

location.href = '@Url.Action("printtopdf","batch",new {}))' + '?allocationId=' + unSelected;

Edit:

The way I'm creating the link assumes that the link will not have any other queryString parameter, otherwise, you'll need to conver the '?' to '&'

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.