I’m trying to redirect to another page by calling an action in controller with a specific parameter. I’m trying to use this line:
window.open('<%= Url.Action("Report", "Survey",
new { id = ' + selectedRow + ' } ) %>');
But I couldn’t make it work; it gives the following error:
CS1012: Too many characters in character literal.
Can’t I generate the action URL this was on the client side? Or do I have to make an Ajax call by supplying the parameter and get back the needed URL? This doesn’t seem right, but I want to if it’s the only way.
Is there an easier solution?
Remember that everything between <% and %> is interpreted as C# code, so what you’re actually doing is trying to evaluate the following line of C#:
C# thinks the single-quotes are surrounding a character literal, hence the error message you’re getting (character literals can only contain a single character in C#)
Perhaps you could generate the URL once in your page script – somewhere in your page HEAD, do this:
That’ll give you a Javascript string containing the URL you need, but with PLACEHOLDER instead of the number. Then set up your click handlers as:
i.e. when the handler runs, you find the PLACEHOLDER value in your pre-calculated URL, and replace it with the selected row.