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.

1
  • Check here Commented May 19, 2017 at 6:36

3 Answers 3

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>

Sign up to request clarification or add additional context in comments.

Comments

1

Why can't you use?:

function createNewGuid() {
   return guid();
}

Comments

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>

3 Comments

ok this is the solution ive been looking for thanks. but im still thinking why Guid.NewGuid() wont work should do the same
@ChrisG well i dont see the point in helping you figure out why the Guid.NewGuid() dont work if you change mark answers often
Hahaha sry i thoutgh i can mark more than one answer as right

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.