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

javascript - Get guid values with js and razor - Stack Overflow

programmeradmin8浏览0评论

I have an Asp.Net mvc 3 project I'm using razor, and need to generate guids in javascript

I was trying this:

<script type="text/javascript">
$(document).ready(function () {
    function getNewGuid() {
        return '@Guid.NewGuid()';
    }

I'm using inside the click event for a button, but the second call to the function is returning the same value

What should I do for reevaluating the function with each call?

I have an Asp.Net mvc 3 project I'm using razor, and need to generate guids in javascript

I was trying this:

<script type="text/javascript">
$(document).ready(function () {
    function getNewGuid() {
        return '@Guid.NewGuid()';
    }

I'm using inside the click event for a button, but the second call to the function is returning the same value

What should I do for reevaluating the function with each call?

Share Improve this question asked Jul 19, 2011 at 20:55 Rodrigo JuarezRodrigo Juarez 1,7951 gold badge24 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

The

@Guid.NewGuid()

is evaluated server-side when the page is rendered, so you will always get the same value.

You need a Javascript Guid library from somewhere.

Try the accepted answer to this question.

While you could make an Ajax call to the server, it's pretty pointless if all you're after is a unique value that could be generated far more efficiently client-side.

Create a controller then use JSON to return a new Guid from the server. Then use $().ajax to get the value.

If you don't want to ask the server for one use the following answer Create GUID / UUID in JavaScript?

发布评论

评论列表(0)

  1. 暂无评论