最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to assign c# guid value to javascript variable using razor viewmodel in asp.net mvc? - Stack Overflow

programmeradmin2浏览0评论

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.

Share Improve this question asked Sep 27, 2013 at 21:57 Aleksei ChepovoiAleksei Chepovoi 3,9559 gold badges42 silver badges77 bronze badges 2
  • 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
Add a comment  | 

4 Answers 4

Reset to default 8

Enclose @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';
发布评论

评论列表(0)

  1. 暂无评论