如何使用视图模型填充下拉列表

时间:2014-05-01 12:58:51

标签: asp.net-mvc asp.net-mvc-4

我目前正在使用ViewBag将数据传递到DropDown列表。 我想使用模型,但我无法使用它。这就是我所拥有的:

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; }
    }   

这很容易吗?谢谢

2 个答案:

答案 0 :(得分:2)

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)

答案 1 :(得分:1)

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

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);
}

并在您看来 -

@model GiftListItems

@Html.DropDownList(
    "catID",
    Model.Categories, 
    new { onchange = "this.form.submit();" }
)

现在你的View模型应该是这样的 -

public class GiftListItems
    {        
        public IEnumerable<SelectListItem> Categories { get; set; }
        // public IEnumerable<Category> Categories { get; set; }  --
    }