简体   繁体   English

ASP.NET MVC通过buttonclick创建新的Guid

[英]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. 我试图每次点击按钮wiuthoput重载整个视图时都会生成一个新的GUID,我不知道该怎么做。 This is the function 这是功能

Guid.NewGuid()

by clicking a button in my Razorview. 通过单击我的Razorview中的按钮。 I tried it in javascript 我在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. 每次我单击按钮时,该方法都被赋予相同的GUID。 I also tried it in MVC Razor with 我也在MVC Razor中尝试过

return "@Guid.NewGuid()"

and still gets the same result. 仍然得到相同的结果。

You can do Generate guid in javascript. 您可以在javascript中执行Generate guid。 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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