![](/img/trans.png)
[英]Pass variable value calling c# behind function with javascript in asp.net
[英]Asp.net pass jQuery variable to C# function
我在Asp.Net的剃須刀視圖中放入了以下代碼:
@section MyScripts {
<script type="text/javascript">
$("button").click(function () {
var myVariable = this.id;
@{
var Line1 = "Software";
var Gruppe2 = Model.Where(x => x.Line1 == "Software").GroupBy(v => v.Line2).ToList();
}
});
</script>
}
如您所見,myVariable通過jQuery從“按鈕單擊事件”接收ID。
因此,可以說變量值為“ Software”。
如何將這個變量傳遞給jQuery中的C#代碼?
將Ajax調用添加到您的click事件中,以對您的MVC操作執行POST / GET,如下所示:
$.ajax({
async: false,
cache: false,
url: '@Url.Action("MyAction", "MyController")',
type: 'GET',
dataType: 'json',
data: myJsonString }
);
您的操作只會返回json(或部分視圖,具體取決於返回類型)
[HttpGet]
public JsonResult MyAction()
{
string result = [some model data]
var json = Json(result, JsonRequestBehavior.AllowGet);
return json;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.