如何将JQuery变量传递给Razor? [英] How to pass JQuery variables to Razor?
本文介绍了如何将JQuery变量传递给Razor?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有JQuery代码:
I have the JQuery code:
$(document).ready(function() {
$('.LikeArea').click(function() {
var num = parseInt(this.html());
num++;
elem.html(num);
});
});
现在我想将jQuery变量 num 传递给Razor所以我可以使用新值更新数据库。
Now I want to pass the jQuery variable num to Razor so I can update the database with the new value.
推荐答案
Razor是一个视图引擎。当你说你想把一个jQuery变量传递给Razor时,不确定你是什么意思,因为Razor在任何javascript之前在服务器上运行。您可以使用AJAX向服务器端模板发送请求:
Razor is a view engine. Not really sure what you mean when you say that you want to pass a jQuery variable to Razor because Razor runs on the server before any javascript. You could use AJAX though to send a request to a server side template:
$(document).ready(function() {
$('.LikeArea').click(function () {
var num = parseInt(this.html());
num++;
elem.html(num);
$.ajax({
url: '/foo.cshtml',
type: 'POST',
data: { num: num },
success: function(result) {
// TODO: do something with the result returned by the
// foo.cshtml template
}
});
});
});
会向 /foo.cshtml发送一个AJAX请求
模板,你可以在其中获取这样的变量:
which would send an AJAX request to the /foo.cshtml
template in which you can fetch the variable like this:
@{
var num = Request["num"];
}
...
这篇关于如何将JQuery变量传递给Razor?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文