[英]How do I call a post actionresponse passing the selected dropdown item as a parameter from javascript within a view
How do I call a post actionresponse passing the selected dropdown item as a parameter from javascript within a view within asp.net, razor, mvc 我如何调用后动作响应,将选定的下拉项作为参数从asp.net,razor,mvc内的视图中的javascript中传递
Basically, as I'm sure you can tell from the below code, I'm trying to accomplish the above without much success. 基本上,正如我确定您可以从下面的代码中看出来的那样,我正在尝试完成上述工作,但没有取得太大的成功。 What should I change to be successful?
我应该怎样改变才能成功? Thanks
谢谢
<script type="text/javascript">
$(function() {
$("#mydropdown").change(function() {
var selectedItem = $(this).val();
$.ajax({
url: '@Url.Action("DoStuff", "MainController")',
type: "Post",
data: { name: selectedItem },
success: function () {
alert('success');
}
});
});
});
[HttpPost]
public ActionResult DoStuff(String Selecteditem)
{
CIModel CIModellList = CILHelper.ImportFunc(Selecteditem);
return View(CIModellList);
}
As of now, nothing happens whem the dropdown is changed. 截至目前,下拉列表没有任何变化。 Although if I remove the ajax function, and replace this with an alert.
虽然如果我删除了ajax函数,并用警报替换它。 Then the alert in this case does appear.
然后,在这种情况下会出现警报。
At this point, both the actionresult and the controller appear as though they do not exist (red) within the project even though both do and are named as stated. 在这一点上,即使结果和控制器都按指定的方式命名,动作结果和控制器也似乎都在项目中不存在(红色)。
The c# seems to suggest neither the action or the controller actually exist, giving the errors "cannot resolve action" or "cannot resolve controller" C#似乎建议操作或控制器都不存在,给出错误“无法解决操作”或“无法解决控制器”
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
</head>
<body>
<select id="mydropdown">
<option value="a">a</option>
<option value="b">b</option>
<option value="c">c</option>
<option value="d">d</option>
</select>
<script type="text/javascript">
$(function() {
$("#mydropdown").change(function() {
var selectedItem = $(this).val();
$.ajax({
url: '@Url.Action("DoStuff", "MainController")',
type: "Post",
data: { name: selectedItem },
success: function () {
alert('success');
}
});
});
});
</script>
</body>
</html>
[HttpPost]
public ActionResult DoStuff(string name)
{
CIModel CIModellList = CILHelper.ImportFunc(name);
return View(CIModellList);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.