从javascript代码中检索变量值并将其传递给我的控制器 [英] retrieve a variable value from javascript code and pass it to my controller
本文介绍了从javascript代码中检索变量值并将其传递给我的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助,如何从javascript代码中检索变量值并将其传递给我的控制器,我正在使用mvc 5 asp.net,我是初学者
hi every i need help about how to retrieve a variable value from javascript code and pass it to my controller, i'm using mvc 5 asp.net and i'm a beginner
推荐答案
一个选项是通过ajax调用传递这个值(使用JQuery)。
在Javascript中,使用这个
One option is to pass this value via an ajax call (using JQuery).
In Javascript, use this
function getVal(id) {
.ajax({
url:'@ Url.Action(Action1,Controller)',
type:' GET',
dataType:'json',
cache:false,
data:{id:id},
success:function(person){
//做点什么
}
});
}
.ajax({ url: '@Url.Action("Action1", "Controller")', type: 'GET', dataType: 'json', cache: false, data: { id: id }, success: function(person) { //Do something } }); }
在您的控制器中,您将获得 id
-
In your controller, you will get id
-
[HttpGet]
public ActionResult Action1(string id)
{
//Do something
}
我想你必须先学习'JavaScript'或'JQuery'。并且还学习AJAX,因为它经常在MVC中使用。
Hi, I think you must first learn 'JavaScript' or 'JQuery'. And also learn AJAX, as it is frequently used in the MVC.
//For fetching the variable value using JavaScript
var a = document.getElementById("variableID").value;
这篇关于从javascript代码中检索变量值并将其传递给我的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文