我正在尝试根据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] 删除。
我来说两句