Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# 如何从asp.net mvc3中的下拉列表访问选定项_C#_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

C# 如何从asp.net mvc3中的下拉列表访问选定项

C# 如何从asp.net mvc3中的下拉列表访问选定项,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我使用asp.net mvc3,并使用以下模型填充创建视图 型号 public class CategoryModel { public int Id { get; set; } public string Name { get; set; } public string URL { get; set; } public string Description { get; set; } public string Logo { get; set;

我使用asp.net mvc3,并使用以下模型填充创建视图

型号

public class CategoryModel
{
    public  int Id { get; set; }
    public string Name { get; set; }
    public  string URL { get; set; }
    public  string Description { get; set; }
    public  string Logo { get; set; }
    public  bool IsActive { get; set; }
    public  bool isPopular { get; set; }
    public IList<Category> Parentcategories { get; set; }

}
谢谢, Ahsan

试试这个:

public ActionResult Create(string parentcategories, CategoryModel model , HttpPostedFileBase file)

parentcategories
将包含选定的
选项
值。

详细信息:您可以直接从您的模型访问它

[HttpPost]
public ActionResult Create( CategoryModel model , HttpPostedFileBase file)
{
      var selectedCategory = model.parentcategories;  // something like that
}

正如前面提到的,您应该使用DropDownListFor:
1.用
public int ParentCategoryId{get;set;}
字段追加您的模型。
2.不要使用@Html.DropDownList,而是使用:
@Html.DropDownListFor(m=>m.ParentCategoryId,新选择列表(…)

3.服务器端可以保持不变:

[HttpPost]
public ActionResult Create(CategoryModel model)
{
   // 
}
其中,
model.ParentCategoryId
将具有选定的项目值。
还请注意,您可以首先为视图设置“选定项”值:

public ActionResult Index()
{
  var model = CategoryModel();
  ...
  model.ParentCategoryId = some_selected_value;
  return View(model);
}

是否有任何方法可以使用
CategoryModel
?@Smartboy是的,请使用属性,然后为此属性编写
DropDownListFor
[HttpPost]
public ActionResult Create(CategoryModel model)
{
   // 
}
public ActionResult Index()
{
  var model = CategoryModel();
  ...
  model.ParentCategoryId = some_selected_value;
  return View(model);
}