-1

Trying to use a viewbag on a create form (view) to let the user pick an "item" on the dropdown. The dropdown displays and shows correct number of rows (tested) but only displays the first item for the X number of rows. https://i.imgur.com/2179GTD.png "Image1"

Code of the view controller below as I didn't find any answers to this.

List<SelectListItem> Contracts = (from a in 
db.v_xxx_yyyy_aaaa_contracts.Where(m => m.GroupID.ToString() == 
@group).Distinct().OrderBy(a => a.ContractID).ToList()
                                              select new SelectListItem()
                                              {
                                                  Value = a.ContractID,
                                                  Text = a.ContractID
                                              }).ToList();

ViewBag.ContractID = Contracts;

2 Answers2

0

Try something like

var contracts = db.v_xxx_yyyy_aaaa_contracts.Where(m => m.GroupID.ToString() == @group).Distinct().OrderBy(a => a.ContractID);

ViewBag.ContractID = new SelectList(contracts, "ContractID", "ContractID"); // here second one for display
  • hi @ashik, thank you for suggesting that approach but unfortunately that is not working either. The problem is not on the ViewBag itself but on the query, on the var "contracts", is returning X instances of item1.Isn't that the way to do a simple: Select ContractID from v_xxx_yyyy_aaaa_contracts where groupID = X – Marcos Ferreira Jan 15 '18 at 13:22
  • a simple ViewBag.ContractID = new SelectList(db.v_xxx_yyyy_aaaa_contracts.Where(m => m.GroupID == @group), "ContractID","ContractID"); is only showing the first ContractID of a group.. repeating it over the X times that group has a contract associated. – Marcos Ferreira Jan 15 '18 at 14:47
0

The solution I found for this specific problem is found here!

Had to bypass the viewmodel with ViewData (SelectList) for it to work as I wanted.