Asp.net mvc 如何使用视图模型填充下拉列表

Asp.net mvc 如何使用视图模型填充下拉列表,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我目前正在使用ViewBag将数据传递到下拉列表。 我想用一个模型来代替,但我不能让它工作。以下是我所拥有的: public ActionResult Index(int? catID) { GiftListItems viewModel = new GiftListItems { Categories = **how do I use this property for DropDown li

我目前正在使用ViewBag将数据传递到下拉列表。 我想用一个模型来代替,但我不能让它工作。以下是我所拥有的:

public ActionResult Index(int? catID)
        {   
            GiftListItems viewModel = new GiftListItems
            {
                Categories = **how do I use this property for DropDown list ?**
            };

            // using this for DropDown list now :  
            var query = _categoryRepository.Table 
                                    .Select(x => new { x.Id, x.Name })        
                                    .Distinct()
                                    .OrderBy(x => x.Name);
            ViewBag.Values = new SelectList(query.AsEnumerable(), "Id", "Name"); 

            return View(viewModel); 
        }    

    In View :
    @Html.DropDownList("catID", (SelectList)ViewBag.Values, new { onchange = "this.form.submit();" })

    Model :
  public class GiftListItems
    {        
        public IEnumerable<Category> Categories { get; set; }
    }   
public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}
公共行动结果索引(int?catID) { GiftListItems viewModel=新的GiftListItems { Categories=**如何将此属性用于下拉列表** }; //现在将此用于下拉列表: var query=\u categoryRepository.Table .Select(x=>new{x.Id,x.Name}) .Distinct() .OrderBy(x=>x.Name); ViewBag.Values=new SelectList(query.AsEnumerable(),“Id”,“Name”); 返回视图(viewModel); } 鉴于: @Html.DropDownList(“catID”,(SelectList)ViewBag.Values,新建{onchange=“this.form.submit();”) 型号: 公共类礼物列表 { 公共IEnumerable类别{get;set;} } 这容易吗?谢谢

public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}
示例:

public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}
@Html.DropDownListFor(c=>c.SelectedItem,Model.SelectListItemListObj)
 public class VmSysCategoryModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
 public class GiftListItemsDropDown
        {
            public int SelectedCategoryId { get; set; }
            public IEnumerable<VmSysCategoryModel> Categories { get; set; }
            public IList<SelectListItem> SelectListItemListObj
            {
                get
                {
                    var list = (from item in Categories
                        select new SelectListItem()
                        {
                            Text = item.Id.ToString(CultureInfo.InvariantCulture),
                            Value=item.Name
                        }).ToList();
                    return list;
                }
                set { }
            }
        }
public ActionResult Index(int? catID)
        {

            var listCategories = _categoryRepository.Table
                .Select(x => new {x.Id, x.Name})
                .Distinct()
                .OrderBy(x => x.Name);

            var obj = new GiftListItemsDropDown()
            {
                Categories = Mapper.Map<IList<listCategories>, IList<VmSysCategoryModel>>(listCategories)
                //here you mast to map from domain to viewmodel 
            };

            return View(obj);
        }
@model GiftListItemsDropDown
@Html.DropDownListFor(c=>c.SelectedCategoryId ,Model.SelectListItemListObj)
型号:

public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}
@Html.DropDownListFor(c=>c.SelectedItem,Model.SelectListItemListObj)
 public class VmSysCategoryModel
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
 public class GiftListItemsDropDown
        {
            public int SelectedCategoryId { get; set; }
            public IEnumerable<VmSysCategoryModel> Categories { get; set; }
            public IList<SelectListItem> SelectListItemListObj
            {
                get
                {
                    var list = (from item in Categories
                        select new SelectListItem()
                        {
                            Text = item.Id.ToString(CultureInfo.InvariantCulture),
                            Value=item.Name
                        }).ToList();
                    return list;
                }
                set { }
            }
        }
public ActionResult Index(int? catID)
        {

            var listCategories = _categoryRepository.Table
                .Select(x => new {x.Id, x.Name})
                .Distinct()
                .OrderBy(x => x.Name);

            var obj = new GiftListItemsDropDown()
            {
                Categories = Mapper.Map<IList<listCategories>, IList<VmSysCategoryModel>>(listCategories)
                //here you mast to map from domain to viewmodel 
            };

            return View(obj);
        }
@model GiftListItemsDropDown
@Html.DropDownListFor(c=>c.SelectedCategoryId ,Model.SelectListItemListObj)

您可以像这样使用视图模型-

public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}
public ActionResult NewsEdit(int ID, dms_New dsn)
{
    var query = _categoryRepository.Table 
                          .Select(x => new { x.Id, x.Name })        
                          .Distinct()
                          .OrderBy(x => x.Name)).ToList();

    GiftListItems viewModel = new GiftListItems
    {
        Categories = query.Select(x => new SelectListItem
        {
            Value = x.ID.ToString(),
            Text = x.Name
        })
    };
    return View(viewModel);
}
public class GiftListItems
    {        
        public IEnumerable<SelectListItem> Categories { get; set; }
        // public IEnumerable<Category> Categories { get; set; }  --
    }
在你看来——

public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}
@model GiftListItems

@Html.DropDownList(
    "catID",
    Model.Categories, 
    new { onchange = "this.form.submit();" }
)
现在您的视图模型应该如下所示-

public class Model
{
public int SelectedItem{get;set;}
public IList<DropDownObj> ListObj{get;set;
public IList<SelectListItem> SelectListItemListObj{get;set;}
        {
            get
            {
                   var list = (from item in ListObj
                            select new SelectListItem()
                            {
                                Text = item.Id.ToString(CultureInfo.InvariantCulture),
                                Value item.Name
                            }).ToList();
                return list;
            }
            set{}
        } 
} 

public class DropDownObj
{
   public int Id{get;set;}
   public string Name{get;set;
}
public ActionResult NewsEdit(int ID, dms_New dsn)
{
    var query = _categoryRepository.Table 
                          .Select(x => new { x.Id, x.Name })        
                          .Distinct()
                          .OrderBy(x => x.Name)).ToList();

    GiftListItems viewModel = new GiftListItems
    {
        Categories = query.Select(x => new SelectListItem
        {
            Value = x.ID.ToString(),
            Text = x.Name
        })
    };
    return View(viewModel);
}
public class GiftListItems
    {        
        public IEnumerable<SelectListItem> Categories { get; set; }
        // public IEnumerable<Category> Categories { get; set; }  --
    }
公共类礼物列表
{        
公共IEnumerable类别{get;set;}
//公共IEnumerable类别{get;set;}--
}

谢谢MD,但我不知道如何在我的代码中实现这一点(目前仍在经历学习的痛苦)。好的,我将试着举个例子感谢代码人员。让我看看我是否能实现它。我把这两个都用到了工作中,并通过学习这两个代码示例学到了很多东西。谢谢你们两位的帮助。我应该接受哪个答案?我可以接受这两个吗?请检查我的答案并发表评论…我已经编辑了我的答案。。。请检查一下。。。