简体   繁体   English

如何通过视图中的javascript将选定的下拉项作为参数传递来调用发布动作响应

[英]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);
    }

You must change your name of the parameter to SelectedItem that matches the parameter your action method and stringify your json data before passing like, 您必须先将参数名称更改为SelectedItem,以使其与您的操作方法的参数匹配,并对json数据进行字符串化,然后再传递,

JSON.stringify({ SelectedItem :selectedItem })

See this for reference. 请参阅作为参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在$ .post()调用中引用对象 - How do I reference the object from within a $.post() call 如何根据选定的下拉项目生成项目列表? - How do I generate a list of items based on selected dropdown item? 选择下拉菜单后,如何通过sql数据库中的数据弹出jquery叠加层,如何发布信息? - How do I POST when a dropdown is selected then popup a jquery overlay filed with data from a sql database? 如何使用javascript将选定的项目放在php post数组中? - How can I put a selected item in a php post array with javascript? 如何使用JavaScript在JSP页面上的下拉列表中呈现选定值的属性? - How do I render attributes for a selected value from a dropdown, on a JSP page, using JavaScript? 如何通过传递 object 作为参数来调用 javascript/typescript 构造函数? - How can I call a javascript/typescript constructor by passing an object as parameter? 如何从Bootstrap下拉菜单中显示所选项目? - How to display selected item from Bootstrap dropdown? 我如何在 php 和 javascript 中发布选定的选项 - how do i post selected options in php and javascript 如何从选定的ajax下拉PHP插入数据 - How do I insert data from selected ajax dropdown PHP 将选定的下拉字符串值从表中传递给控制器 - Passing selected dropdown string value from table in view to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM