Js (jquery) .NET(mvc) 網頁傳接值 (單一字串)
(1) _layout 宣告 action controller 放在 js 變數
(2) Js $ajax Post => data: { 'mydata': "toreoo"}
(3) C# controller 接值 => public void setp06_onchange(string mydata){ }//mydata 接到值
----------------------------------------------------------------------
_LayoutPage.cshtml
----------------------------------------------------------------------
<!DOCTYPE html>
<html>
<head>
<!--jquery customer-->
<script src="~/Scripts/test.js?v=@DateTime.Now.Millisecond" type="text/javascript"></script>
<script type="text/javascript">
var Url_setp06_onchange_apply = '@Url.Action("my_action","my_controller")';
</script>
</head>
<body>
</body>
</html>
----------------------------------------------------------------------
JS
----------------------------------------------------------------------
$("#test").mousedown(function () {
$.ajax({
type: "POST",
url: Url_setp06_onchange_apply,
data: {
'mydata': "toreoo"
},
success: function (data) {
console.log("s");
},
error: function (xhr, status, p3, p4) {
console.log("e");
}
});
});
----------------------------------------------------------------------
my_controller
----------------------------------------------------------------------
public void setp06_onchange(string mydata)
{
//mydata 接到值
}