基于 ASP.NET MVC 中的硬编码下拉列表选择显示数据库中的项目列表

拉吉

我正在尝试根据dropdownlist在 ASP.NET MVC 应用程序中硬编码的选择显示数据库中的项目列表

我的 Controller

 public ActionResult ListofItems()
            {
                ListofClassClassHandle listofClassClassHandle = new ListofClassClassHandle ();
                return View(listofClassClassHandle.LeadingAll());
            }

ListofClassClassHandle 班级

public List<Leading> LeadingAll()
        {
            clsUtilities clsUtilities = new clsUtilities();
            DataSet ds;
            List<Leading> leading = new List<Leading>();
            string sSQL;
            sSQL = "exec GetLeading 'NZ'";
            ds = clsUtilities.GetDataSet(sSQL);
            DataTable dataTable = ds.Tables[0];
            foreach(DataRow dr in dataTable.Rows)
            {
                leading.Add(
                    new Leading
                    {
                        RankId = Convert.ToInt32(dr["RankId"]),
                        Name = Convert.ToString(dr["Name"]),

                    }               
                    ); 
            }

Leading 班级

public class Leading
    {
        public int RankId { get; set; }
        public string Name{ get; set; }   
        public Countries AllCountries { get; set; }

    }
public enum Countries
    {
        New_Zealand,
        Australia      
    }

Leading 看法

 @Html.DropDownList("AllCountries",
        new SelectList(Enum.GetValues(typeof(Countries))),
        "Select Country",
         new { @class = "form-control", style = "width: 150px;" })

<table class="table">
    <tr>
        <th>
            @Html.DisplayName("Rank")
        </th>
        <th>
            @Html.DisplayName("Name")
        </th>


    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RankId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>

        </tr>
    }

</table>

我想根据下拉国家/地区列表选择显示列表。下拉列表是硬编码的,其中数据列表是从数据库填充的。

请指导我。我不知道如何做到这一点?非常感谢任何帮助或指导。

许久

您可以将下拉选择传递给您的 GET 操作方法,然后从那里传递给您的数据访问方法,并使用它来获取过滤后的数据集。

首先,向您的操作方法添加一个参数。

public ActionResult ListofItems(string AllCountries="")
{
    var h = new ListofClassClassHandle();
    return View(h.LeadingAll(AllCountries));
}

并更新LeadingAll方法以接受此参数。更新您的数据访问代码以使用country变量内的值。

public List<Leading> LeadingAll(string country)
{
    // to do : use the value of country to call the stored procedure
    // to do : return list of Leading objects
}

现在在您的视图中,您可以将下拉列表form与提交按钮一起保存在标签中。将表单标签的 action 属性值设置为您的ListOfItems操作方法并将表单method属性设置GET

@using (Html.BeginForm("ListOfItems", "Home", FormMethod.Get))
{
    @Html.DropDownList("AllCountries",
            new SelectList(Enum.GetValues(typeof(Countries))),
            "Select Country",
            new { @class = "form-control", style = "width: 150px;" })
    <input type="submit" value="Filter" />
}

当用户从 SELECT 元素中选择一个选项并单击提交按钮时,它将使用查询字符串(例如:)中的选择进行 GET 调用,/ListOfItems/?AllCountries=Australia您的操作方法代码将使用此值通过将其传递给该国家/地区来获取该国家/地区的数据您的数据访问方法。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

下拉列表中的Asp.net MVC多个字段无法回发到数据库

根据asp.net mvc5中数据库的值设置下拉列表的值

如何基于特定值查询数据库中的列MVC ASP.NET Core

选择下拉列表并保存到数据库asp.net mvc

ASP.NET 5 / MVC 6中基于约定的绑定

如何基于asp.net mvc4中的模型列表填充列表框?

ASP.NET MVC中基于角色的访问控制(RBAC)与基于声明的访问控制(CBAC)

基于下拉列表显示和隐藏 asp mvc 5

通过选择下拉列表 ASP.NET Core MVC 从数据库填充多个文本框

基于ASp.NET MVC中的下拉信息重定向页面

在ASP.NET MVC上从数据库显示项目

如何使用asp.net mvc在数据库中保存级联下拉列表项

ASP.NET MVC5 触发 onchange 事件时从数据库填充下拉列表

在ASP.NET MVC中显示基于身份验证状态的视图

基于asp.net mvc中的值的复选框显示/隐藏字段

ASP.NET MVC / SQL Server中基于角色的实用数据访问控件

试图显示基于DropDownList选择的动态WebGrid-ASP.Net MVC4

在 ASP.NET MVC 中填充下拉列表的正确方法

如何从ASP.NET MVC中的枚举创建下拉列表?

在ASP.NET MVC中创建下拉列表

如何在Asp .net MVC中验证下拉列表

如何从ASP.NET MVC中的枚举创建下拉列表?

ASP.NET MVC中的从属下拉列表

如何在 ASP.Net MVC 中创建下拉列表

如何在ASP.NET Core MVC中基于用户表中的字段添加声明?

根据 ASP.NET MVC 5 中另一个下拉列表中的选择填充下拉列表

如何基于querystring值更改ASP.Net MVC 5中的主题?

ASP.NET Core MVC 2.0中基于路径的身份验证

如何基于注释[key]标签从ASP NET MVC模型对象中识别主键参数?