[英]Passing data from javascript to action method in asp.net MVC
I have placed a bootstrap toggle switch in my application Now i want is to send the On and Off values to my action method 我已经在应用程序中放置了一个引导切换开关,现在我想将On和Off值发送到我的操作方法
Bellow is my razor syntax 波纹管是我的剃刀语法
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset style="height:60px">
<legend style="text-align:center; font-size:large; font-family:'Times New Roman'; background-color:#C8E6C9; color:red">Remote On/Off</legend>
<input id="test_id" name="cmdName" type="checkbox" checked data-toggle="toggle">
</fieldset>}
For passing data to controller from JS
i have searched many articles and found that ajax is the way to do it 为了将数据从
JS
传递到控制器,我搜索了许多文章,发现ajax是做到这一点的方法
Bellow is my script for ajax inside JS 贝娄是我在JS中使用Ajax的脚本
<script>
var cmdName = '@Session["cmdName"]';
$("#test_id").on("change", function (event) {
if ($(this).is(":checked")) {
$.ajax({
url: '@Url.Action("MultiGraph")',
type: 'Post',
data: 'On',
success: function () {
alert(data)
}
});
} else {
$.ajax({
url: '@Url.Action("MultiGraph")',
type: 'Post',
data: 'Off',
success: function () {
alert(data)
}
});
}
}); </script>
I have also used session variable but getting null value in it 我也使用了会话变量,但是却得到了空值
Bellow is my controller code 波纹管是我的控制器代码
public ActionResult MultiGraph(string search, string start_date, string End_date, string cmdName, int? page)
{
//search contain serial number(s)
//cmdName is for input checkbox
}
Bellow is the image for my switch button 波纹管是我的切换按钮的图像
When i switch it to Off then this Off string should be sent to my action method and vise versa 当我将其切换为“ 关闭 ”时,应将此“关闭”字符串发送到我的操作方法,反之亦然
Updated Code 更新的代码
After reading comments i have done the following changes to my code 阅读评论后,我对代码进行了以下更改
I have added a new action method of type Post
我添加了类型为
Post
的新操作方法
[HttpPost]
public ActionResult ToggleSwitch (string search, string cmdName)
{
List<SelectListItem> items = new List<SelectListItem>();
var dtt = db.ADS_Device_Data.Select(a => a.Device_Serial_Number).Distinct().ToList();
foreach (var item in dtt)
{
if (!string.IsNullOrEmpty(item))
{
items.Add(new SelectListItem { Text = item, Value = item });
}
}
ViewBag.search = items;
return View();
}
Bellow are changes in my razor 贝娄是我剃刀的变化
$("#test_id").on("change", function (event) {
if ($(this).is(":checked")) {
$.ajax({
url: '@Url.Action("ToggleSwitch")',
type: 'Post',
data: '{"cmdName": "On"}',
success: function () {
alert(data)
}
});
} else {
$.ajax({
url: '@Url.Action("ToggleSwitch")',
type: 'Post',
data: '{"cmdName": "Off"}',
success: function () {
alert(data)
}
});
}
});
But still i get no alert message, when i inspect element i found this error 但是,当我检查元素时,仍然发现此错误,但仍未收到任何警报消息
I am stuck to this problem from almost 2 days 我将近2天都陷入这个问题
Any help would be appreciated 任何帮助,将不胜感激
You need to use the $
character to invoque jQuery
functions and pass your data from page to controller with the same name you defined in the action method using Json
notation: 您需要使用
$
字符来调用jQuery
函数,并使用与您在Json
表示法中在action方法中定义的名称相同的名称将数据从页面传递到控制器:
'{"cmdName": "On"}',
$.ajax({
url: '@Url.Action("ToggleSwitch")',
type: 'Post',
data: '{"cmdName": "On"}',
success: function () {
alert(data)
}
Furthermore, you might need to decorate your mvc action whith the [HttpPost]
attribute. 此外,您可能需要使用
[HttpPost]
属性来修饰mvc操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.