Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从Dropdownlist中获取值?_C#_Asp.net Mvc - Fatal编程技术网

C# 如何从Dropdownlist中获取值?

C# 如何从Dropdownlist中获取值?,c#,asp.net-mvc,C#,Asp.net Mvc,我有两个DropDownList,它们通过外键相互关联。 类别和子类别(使用categoryid作为外键)。 我正在从数据库中检索类别,并将其填写为: DB类 public class DB { public static List<SelectListItem> Categories { get{ List<SelectListItem> list = new List<SelectLi

我有两个
DropDownList
,它们通过外键相互关联。 类别和子类别(使用
categoryid
作为外键)。 我正在从数据库中检索类别,并将其填写为: DB类

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)
    {...}
 }
我是ASP.NET MVC的初学者,我正在使用ADO.NET访问数据库。 现在如何获取所选项目的值字段,以便将其放入我的
DB.subcategory WithCategoryId(categoryId)
中,以检索跨所选类别映射的子类别

控制器

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

当下拉列表更改为

 @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)

添加有关控制器和视图的代码too@HienNguyen好的,我已经编辑了我的问题..您希望使用哪种方法根据您对数据库选择的值执行更新操作,你能展示它吗?@SalahAkbari现在我只想当我在类别下拉列表中选择值时,子类别下拉列表会被填充。如果你将
@Html.DropDownList(“Category”
)更改为
@Html.DropDownList(“CategoryId”
,会发生什么?它们需要相同,以便模型绑定器按预期工作,
public ActionResult Index(string category)