简体   繁体   中英

ASP.NET MVC Create a new Guid by buttonclick

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.

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> 

Why can't you use?:

function createNewGuid() {
   return guid();
}

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM