简体   繁体   English

如何从下拉列表中获取价值?

[英]How to get value from Dropdownlist?

I have two DropDownList which are related to each other by a foreign key.我有两个DropDownList ,它们通过外键相互关联。 Category and Subcategory(with categoryid as foreign key).类别和子类别(以categoryid作为外键)。 I am retrieving Categories from database and filling them up as: DB CLASS我正在从数据库中检索类别并将它们填充为: 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)
    {...}
 }

Now i can fill Subcategory dropdown with a method which expects categoryId as input:现在我可以使用期望 categoryId 作为输入的方法填充子类别下拉列表:
View看法

 @Html.DropDownList("Category", DB.Categories, "Select Category", new { style = "width:80px" })

 @Html.DropDownList("Subcategory", DB.SubcategoryWithCategoryId(categoryId), "Select Subcategory", new { style = "width:80px" })

I am a beginner in ASP.NET MVC and i am using ADO.NET to access the database.我是ASP.NET MVC的初学者,我正在使用ADO.NET访问数据库。 How can i now get the Selected Item's Value Field so i can fit it into my DB.SubcategoryWithCategoryId(categoryId) to retrieve subcategories mapped across the selected category?我现在如何获取所选项目的值字段,以便将其放入我的DB.SubcategoryWithCategoryId(categoryId)以检索跨所选类别映射的子类别?

Controller Controller

public ActionResult Index()
        {
            return View();
        }

You need handle onchange of dropdown as您需要将下拉菜单的 onchange 处理为

 @Html.DropDownList("Category", DB.Categories, "Select Category", new { style = "width:80px", 
onchange = "location.href='@Url.Action("Index", "Controller", new { category= "yourcategory"})'" })

And add category as paramter of index action并添加类别作为索引操作的参数

public ActionResult Index(string category)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM