C# 如何从asp.net mvc3中的下拉列表访问选定项
我使用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;
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);
}