Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I can't figure it out.
var path = '@Url.Action("index","Survey")?id='
           + @Model.MyGuid+'&languageName=' +selectedValue;
       alert(path.val());

Get the error as the title.
Uncaught SyntaxError: Invalid or unexpected token


EDIT:
Action in controller:
public ActionResult Index(Guid id, string languageName)
       {


What I have tried:

If we change the parameters order and use double quotes. Then the error is gone.
var t = "@Url.Action("index","Survey")?languageName="
                 + selectedValue +"&id=@Model.MyGuid";
Posted
Updated 9-Aug-17 1:25am
v2
Comments
[no name] 9-Aug-17 5:45am    
Where is your controller method? where you are catching this query string? Might be there is any issue. Please also post that method, then we visualize it by taking all scenarios.
Member 12658724 9-Aug-17 7:03am    
Added controller method

1 solution

That's a javascript error and javascript runs off what is sent to the browser (not what is in your cshtml file) so look at the page source.

var path = '@Url.Action("index","Survey")?id='
           + @Model.MyGuid+'&languageName=' +selectedValue;
       alert(path.val());


The above will render as something like this

var path = '/Survey/index?id='
           + {6AAE15E3-E702-4251-A6F1-2A27EBBAA4A0}+'&languageName=' +selectedValue;
       alert(path.val());


Is that valid javascript? No. When it gets to "+ {" it thinks the { is a code block. Now look at your working example

var t = "@Url.Action("index","Survey")?languageName="
                 + selectedValue +"&id=@Model.MyGuid";


View the source and it will be like

var t = "/Survey/index?languageName="
                 + selectedValue +"&id={6AAE15E3-E702-4251-A6F1-2A27EBBAA4A0}";


Is that valid? Yes. The "{ }" is inside a string block (it has quotes around it) so it knows it is a literal string and not a code block.

That might not be the specific issue as we don't know what MyGuid renders as, but it's going to be related to the same root issue.
 
Share this answer
 
Comments
Member 12658724 9-Aug-17 10:39am    
MyGuid renders result as 6AAE15E3-E702-4251-A6F1-2A27EBBAA4A0
No{} around it.
F-ES Sitecore 9-Aug-17 10:45am    
As i said, the fact that you have a GUID outside of a quote is still the issue. It might look like a variable assignment in your cshtml file, but you have to appreciate that it isn't a variable assignment, it is simply generating js to be rendered and executed on the client.
Member 12658724 9-Aug-17 11:10am    
Okay. My actual url likes:
http://localhost:17671/Survey/6AAE15E3-E702-4251-A6F1-2A27EBBAA4A0?languageName=English&id=6AAE15E3-E702-4251-A6F1-2A27EBBAA4A0.
What is supposed to be?
Is it http://localhost:17671/Survey/{6AAE15E3-E702-4251-A6F1-2A27EBBAA4A0}?languageName=English&id={6AAE15E3-E702-4251-A6F1-2A27EBBAA4A0}.
I want to add { }to the url.
Richard Deeming 9-Aug-17 11:04am    
Either:
var path = '@Url.Action("index", "Survey")?id=@Model.MyGuid&languageName=' + encodeURIComponent(selectedValue);

Or:
var path = '@Url.Action("index", "Survey", new { id = Model.MyGuid })&languageName=' + encodeURIComponent(selectedValue);
Member 12658724 9-Aug-17 11:23am    
parsing a meta element's content: ';' is not a valid key-value pair separator. Please use ',' instead

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900