Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Ajax中使用Get方法将参数获取到控制器中_C#_Ajax_Asp.net Mvc_Razor_Combobox - Fatal编程技术网

C# 在Ajax中使用Get方法将参数获取到控制器中

C# 在Ajax中使用Get方法将参数获取到控制器中,c#,ajax,asp.net-mvc,razor,combobox,C#,Ajax,Asp.net Mvc,Razor,Combobox,我在cshtml文件中有这个代码 @using (Html.BeginForm("UserPermission", "UserPermision", new { area = "Admin", id = "cbbDpm", name = "cbbDpm" }, FormMethod.Post)) { @Html.DropDownListFor(m => m.Dept, new SelectList(Model.Dept, "DepartmentName", "Depart

我在cshtml文件中有这个代码

@using (Html.BeginForm("UserPermission", "UserPermision", new { area = "Admin", id = "cbbDpm", name = "cbbDpm" }, FormMethod.Post))
   {
      @Html.DropDownListFor(m => m.Dept, new SelectList(Model.Dept, "DepartmentName", "DepartmentName"), "-- Select Department --", new { @class = "form-control w-100", @id = "cbbDpmt", @name = "cbbDpmt" })
   }
...
<div id="partialDiv">
   @Html.Partial("_Table")
</div>
...
<script>
    $(document).ready(function () {
        $("#cbbDpmt").on("change", function () {
            $.ajax(
                {
                    url: '/Admin/UserPermision/CBBSelectedChange?dpmname' + $(this).val(),
                    type: 'GET',
                    data: "",
                    contentType: 'application/json; charset=utf-8',
                    success: function (data) {
                        $("#partialDiv").html(data);
                    },
                    error: function () {
                        alert("error");
                    }
                });
        });
    });
</script>
在控制器中,我有:

[HttpGet]
public PartialViewResult CBBSelectedChange(string dpmname)
{
 List<AdminUserPermission> userModels = DatabaseServer.ConvertDataTable<AdminUserPermission>(DatabaseServer.Read_Table(@"SELECT * 
                                                          FROM dbo.tblWFX_UserGroup AS gr 
                                                          JOIN dbo.tblWFX_Department AS dpm ON gr.DepartmentName = dpm.DepartmentID 
                                                          WHERE dpm.DepartmentName = " + dpmname, false));
 Session["selectedDept"] = dpmname;
 AdminUserPermissionParty adminUserPermissionParties = new AdminUserPermissionParty();
 adminUserPermissionParties.UserPermisionModel = userModels;
 return PartialView("_Table", adminUserPermissionParties);
}
我想把从cshtml中的组合框中选择的项放入控制器中的函数中。我尝试研究了很多次,但我不能把从cbb中选择的值放入字符串dpmname中。 请帮助我。

您需要使用[FromUri]

并将Ajax调用更改如下:

 $.ajax(
        {
            url: '/Admin/UserPermision/CBBSelectedChange?dpmname=' + $(this).val(),
            type: 'GET',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $("#partialDiv").html(data);
            },
            error: function () {
                alert("error");
            }
        });

请参阅:

非常感谢^^不客气,请单击绿色复选框将答案标记为已接受,以便对其他人有所帮助。@DuyKhơng请阅读我已接受。对不起,我是新人:
 $.ajax(
        {
            url: '/Admin/UserPermision/CBBSelectedChange?dpmname=' + $(this).val(),
            type: 'GET',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $("#partialDiv").html(data);
            },
            error: function () {
                alert("error");
            }
        });