I have a view model with property of Guid
type. I need to assign it to javascript object property and post this object to some action method.
When I write (in javascript):
var partyId = @Model.Id; // "Id" is of Guid type
I get
var partyId = 6abbf77d-ba28-4d8a-87ff-2fa8f8a070c9;
// Uncaught SyntaxError: Unexpected identifier
How can I handle this? I mean assign Id
value to javascript variable.
I have a view model with property of Guid
type. I need to assign it to javascript object property and post this object to some action method.
When I write (in javascript):
var partyId = @Model.Id; // "Id" is of Guid type
I get
var partyId = 6abbf77d-ba28-4d8a-87ff-2fa8f8a070c9;
// Uncaught SyntaxError: Unexpected identifier
How can I handle this? I mean assign Id
value to javascript variable.
- 3 What happens if you enclose @Model.Id within single quotes? – Adam Miller Commented Sep 27, 2013 at 22:00
- post Your answer and I will accept it, cause You were first – Aleksei Chepovoi Commented Sep 27, 2013 at 22:05
4 Answers
Reset to default 8Enclose @Model.Id within quotes.
Your JavaScript output is ultimately going to be a string, so it will formatted as such using quotes:
var partyId = '6abbf77d-ba28-4d8a-87ff-2fa8f8a070c9';
Your current snippet is missing this and is just rendering the raw value.
I'm unaware of any GUID type in JavaScript to parse it between, though, if that's what you're looking for.
You can enclose the model with single or double quotes. You may receive weird parsing errors doing that, but you can typically get rid of them by enclosing the vb/c# code with parentheses.
var partyId = '@(Model.Id)';
Enclose it in quotes?
var partyId = '@Model.Id';