Reputation: 91
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.
Upvotes: 0
Views: 2102
Reputation: 977
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>
Upvotes: 1
Reputation: 27041
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>
Upvotes: 0