[英]How to get value from Dropdownlist?
我有两个DropDownList
,它们通过外键相互关联。 类别和子类别(以categoryid
作为外键)。 我正在从数据库中检索类别并将它们填充为: DB CLASS
public class DB
{
public static List<SelectListItem> Categories {
get{
List<SelectListItem> list = new List<SelectListItem {
new SelectListItem{Text = "Category1" , Value = "categoryId1"},
new SelectListItem{Text = "Category2" , Value = "categoryId2"}
};
return list;
}
public static List<SelectListItem> SubcategoryWithCategoryId(int categoryId)
{...}
}
现在我可以使用期望 categoryId 作为输入的方法填充子类别下拉列表:
看法
@Html.DropDownList("Category", DB.Categories, "Select Category", new { style = "width:80px" })
@Html.DropDownList("Subcategory", DB.SubcategoryWithCategoryId(categoryId), "Select Subcategory", new { style = "width:80px" })
我是ASP.NET MVC的初学者,我正在使用ADO.NET访问数据库。 我现在如何获取所选项目的值字段,以便将其放入我的DB.SubcategoryWithCategoryId(categoryId)
以检索跨所选类别映射的子类别?
Controller
public ActionResult Index()
{
return View();
}
您需要将下拉菜单的 onchange 处理为
@Html.DropDownList("Category", DB.Categories, "Select Category", new { style = "width:80px",
onchange = "location.href='@Url.Action("Index", "Controller", new { category= "yourcategory"})'" })
并添加类别作为索引操作的参数
public ActionResult Index(string category)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.