本文介绍了如何在ASP.NET MVC4中为DropdownListFor创建控制器,从MSSQL中填充数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型代码:
public class PostLeadModel
{
[显示(名称= 项目输入) 0 ]
public string ItemType { get ; set ; }
[显示(名称= 项目类别)]
public string ItemCategory { get ; set ; }
[显示(名称= 项目子类别)]
}
public int SelectedItemCategoryId {获得跨度>; set ; }
public IList< itemcategory> _itemCategories;
public IEnumerable< SelectListItem> ItemCats
{
get
{
var allItemCategories = _itemCategories.Select(f = > new SelectListItem
{
Value = f.ID.ToString(),
Text = f.Name
});
return DefaultItemCats.Concat(allItemCategories);
}
}
public IEnumerable< SelectListItem> DefaultItemCats
{
get
{
return 可枚举.Repeat( new SelectListItem
{
Value = - 1,
Text = 选择一个项目类别
},count: 1 );
}
}
// private List< itemsubcategory> _itemSubCategories;
[显示(名称= Item SubCategories)]
public int SelectedItemSubCategoryId { get 跨度>; set ; }
public IList< itemsubcategory> _itemSubCategories;
public IEnumerable< SelectListItem> ItemSubCats
{
get
{
var allItemSubCategories = _itemSubCategories.Select(f = > new SelectListItem
{
Value = f.ID.ToString(),
Text = f.Name
});
return DefaultItemSubCats.Concat(allItemSubCategories);
}
}
public IEnumerable< SelectListItem> DefaultItemSubCats
{
get
{
return 可枚举.Repeat( new SelectListItem
{
Value = - 1,
Text = 选择一个Item SubCategory
},count: 1 );
}
}
我的视图代码:
@ Html.LabelFor(m => m.SelectedItemCategoryId)
@ Html.DropDownListFor(m => m.SelectedItemCategoryId,Model.ItemCats)
@ Html.ValidationMessageFor(m => ; m.SelectedItemCategoryId)
@ Html.LabelFor(m => m.SelectedItemSubCategoryId)
@ Html.DropDownListFor(m = > m .SelectedItemSubCategoryId,Model.ItemSubCats)
@ Html.ValidationMessageFor(m => m.SelectedItemSubCategoryId);
解决方案
My code for the model:
public class PostLeadModel { [Display(Name = "Item Type")0] public string ItemType { get; set; } [Display(Name = "Item Category")] public string ItemCategory { get; set; } [Display(Name = "Item Sub-Category")] } public int SelectedItemCategoryId { get; set; } public IList<itemcategory> _itemCategories; public IEnumerable<SelectListItem> ItemCats { get { var allItemCategories = _itemCategories.Select(f => new SelectListItem { Value = f.ID.ToString(), Text = f.Name }); return DefaultItemCats.Concat(allItemCategories); } } public IEnumerable<SelectListItem> DefaultItemCats { get { return Enumerable.Repeat(new SelectListItem { Value = "-1", Text = "Select an Item Category" }, count: 1); } } //private List<itemsubcategory> _itemSubCategories; [Display(Name = " Item SubCategories")] public int SelectedItemSubCategoryId { get; set; } public IList<itemsubcategory> _itemSubCategories; public IEnumerable<SelectListItem> ItemSubCats { get { var allItemSubCategories = _itemSubCategories.Select(f => new SelectListItem { Value = f.ID.ToString(), Text = f.Name }); return DefaultItemSubCats.Concat(allItemSubCategories); } } public IEnumerable<SelectListItem> DefaultItemSubCats { get { return Enumerable.Repeat(new SelectListItem { Value = "-1", Text = "Select an Item SubCategory" }, count: 1); } }
My code for the View:
@Html.LabelFor(m=>m.SelectedItemCategoryId)
@Html.DropDownListFor(m =>m.SelectedItemCategoryId, Model.ItemCats)
@Html.ValidationMessageFor(m=>m.SelectedItemCategoryId)
@Html.LabelFor(m=>m.SelectedItemSubCategoryId)
@Html.DropDownListFor(m => m.SelectedItemSubCategoryId, Model.ItemSubCats)
@Html.ValidationMessageFor(m=>m.SelectedItemSubCategoryId);
解决方案
这篇关于如何在ASP.NET MVC4中为DropdownListFor创建控制器,从MSSQL中填充数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!