jquery - 带有下拉选择的 AngularJS

标签 jquery asp.net angularjs ajax

如果我选择下拉文本,则使用 SQL 数据库显示该表。请尝试在这里解决我的代码现在假设我在下拉列表中选择一个文本并单击按钮,然后使用 SQL Server 显示所选的文本表。

<div ng-app="myApp" ng-controller="myCntrl"> user Roles: <select id="dropdown" name="dropdown" ng-model="dropdown">
  <option value="">--Select--</option>
  <option data-ng-repeat="Role in RolesList">{{Role.RoleName}}</option>
</select>
  </br></br>
  <input id="Button1" type="button" class="button" value="button" ng-click="click()"  />      </div>

<script>var app = angular.module("myApp", []);
        app.controller("myCntrl", function ($scope, $http) {               
        $scope.fillList = function () {
            $scope.RoleName = "";
            var httpreq = {
                method: 'POST',
                url: 'WebForm1.aspx/GetList',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                    'dataType': 'json'
                },
                data: {}
            }
            $http(httpreq).success(function (response) {
                $scope.RolesList = response.d;                   
            })
        };
        $scope.fillList();           
    });
</script>

这是我的cs代码

[System.Web.Services.WebMethod()]
    public static List<Names> GetList()
    {
        List<Names> names = new List<Names>();
        DataSet ds = new DataSet();
        using (SqlConnection con = new SqlConnection(@"Data Source=192.168.1.42,1433;Initial Catalog=Harneedi;User ID=chaitanya_t;Password=makrotech"))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "select RoleName from HN_Master_User_Role";
                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                }
            }
        }
        if (ds != null && ds.Tables.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
                names.Add(new Names(dr["RoleName"].ToString()));
        }
        return names;
    }

}
public class Names
{
    public string RoleName;
    public Names(string _RoleName)
    {
        RoleName = _RoleName;
    }
}

}

最佳答案

您的填充下拉代码的代码对我来说看起来很好。虽然您没有指定当用户单击按钮时要执行的操作,但我将向您展示如何获取下拉列表的选定值。

您需要在 Controller 中添加一个可以从按钮调用的行为:-

$scope.fillList(); 
$scope.click = function (selectedValue) {
                     alert(selectedValue);
                }

然后,由于您已经使用 ng-model="dropdown" 绑定(bind)了下拉列表所选值,因此您可以在调用此行为时使用相同的值:-

 <input id="Button1" type="button" class="button" value="button" 
         ng-click="click(dropdown)" />

关于jquery - 带有下拉选择的 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33915999/

相关文章:

javascript - 无法正确格式化 AJAX POST 请求

javascript - 获取url的最后一部分

c# - .NET Core 从 Startup.cs 重定向到自定义错误页面

javascript - 将数据从 View 中的动态文本框发送到 AngularJS Controller

ruby-on-rails - 如何使用 angularjs 和 rails/devise 更新密码?

AngularJS Link 函数没有被调用

javascript - 如何将文本缩放到父 div 插件问题

javascript - 如何在 Google map 上单击打开 jQuery UI 对话框

.net - ASP.NET 的自定义文件扩展名 - 需要帮助!

javascript - 将字符串作为参数传递给 Javascript 中对象的 InnerHtml 属性