0

Im trying to generate a new GUID every time im clicking a button wiuthoput reloading the whole view and i cant figure out what to do. This is the function

Guid.NewGuid()

by clicking a button in my Razorview. I tried it in javascript

 $("#buttonclick").click(function () {
    function createNewGuid() {
        return new Guid.NewGuid();
    }
    var guid = createNewGuid();
    console.log(guid);
}

and this method is just given the same guid every time i click the button. I also tried it in MVC Razor with

return "@Guid.NewGuid()"

and still gets the same result.

smurfo
  • 81
  • 9

3 Answers3

1

Why can't you use?:

function createNewGuid() {
   return guid();
}
Arion
  • 31,011
  • 10
  • 70
  • 88
1

You can do Generate guid in javascript. like the below code by the use of Regular expressions

$("#buttonclick").click(function () {
    var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {  
      var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);  
      return v.toString(16);  
   }); 
  console.log(guid);
  alert(guid);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="buttonclick">Generate Guid</button>
Supraj V
  • 967
  • 1
  • 10
  • 19
0

Is this what you are looking for?

$("#buttonclick").click(function() {
  function createNewGuid() {
    function s4() {
      return Math.floor((1 + Math.random()) * 0x10000)
        .toString(16)
        .substring(1);
    }
    return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
      s4() + '-' + s4() + s4() + s4();
  }
  var guid = createNewGuid();
  console.log(guid);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="buttonclick">guid</button>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77