Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
38
rated 0 times [  43] [ 5]  / answers: 1 / hits: 50899  / 15 Years ago, wed, january 6, 2010, 12:00:00

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?


More From » asp.net

 Answers
23

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#:



Url.Action(Report, Survey, new { id = ' + selectedRow + ' } )


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:



var actionUrl =
'<%= Url.Action(Report, Survey, new { id = PLACEHOLDER } ) %>';


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:



window.open(actionUrl.replace('PLACEHOLDER', selectedRow));


i.e. when the handler runs, you find the PLACEHOLDER value in your pre-calculated URL, and replace it with the selected row.


[#97912] Sunday, January 3, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hasanb

Total Points: 321
Total Questions: 102
Total Answers: 96

Location: Burkina Faso
Member since Fri, Sep 4, 2020
4 Years ago
;