Reputation: 263
I'm currently using a ViewBag to pass data to the DropDown list. I want to use a model instead but I can't get it to work. Here is what I have :
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; }
}
Is this easy to do? Thanks
Upvotes: 1
Views: 208
Reputation: 6656
You can use the view model like this -
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);
}
and in your view -
@model GiftListItems
@Html.DropDownList(
"catID",
Model.Categories,
new { onchange = "this.form.submit();" }
)
And your View model now should look like this -
public class GiftListItems
{
public IEnumerable<SelectListItem> Categories { get; set; }
// public IEnumerable<Category> Categories { get; set; } --
}
Upvotes: 1
Reputation: 1089
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;
}
usage:
@Html.DropDownListFor(c=>c.SelectedItem,Model.SelectListItemListObj)
Example:
Models:
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 { }
}
}
Controller:
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);
}
View:
@model GiftListItemsDropDown
@Html.DropDownListFor(c=>c.SelectedCategoryId ,Model.SelectListItemListObj)
Upvotes: 2