0

I'm working on an asp.net mvc project.

I've been able to combine 2 fields from a table into a drop down list. However, in both the create and index views, I get these "{ }" brackets and I can't figure out how to get rid of them.

Controller: //Get: /Create

var units = db.EquipmentLists
            .Where(s => s.Unit != null)
            .OrderBy(s => s.Unit)
            .Select(s => new
            {
                Description = s.Unit + " - " + s.MakeModel
            }).ToList();

        ViewBag.Units = new SelectList(units); 

//Post: /Create

ViewBag.Units = new SelectList(db.EquipmentLists.OrderBy(x => x.Unit + " - " + x.MakeModel));

Create.cshtml

@Html.DropDownListFor(model => model.Unit, (SelectList)ViewBag.Units, "Select one...", htmlAttributes: new { @class = "form-control" })

Don't know why I'm getting the "{ }" brackets in the views. Just want to get rid of them. Any help would be appreciated.

user2673097
  • 179
  • 1
  • 3
  • 11

3 Answers3

0

Your query is creating an anonymous object with a property named Description.

When you use the SelectList constructor with a single argument (where the parameter is IEnumerable) the method calls .ToString() on each item in the collection to generate the values and display text for each option), and is only suitable where the collection contains simple types (e.g. IEnumerable<string> or IEnumerable<int>)

Change the constructor to specify the property of your object your want for the value and display text using the overload that accepts arguments for dataValueField, and dataTextField

ViewBag.Units = new SelectList(units, "Description", "Description"); 

Alternatively you can create a IEnumerable<SelectListItem> directly using

ViewBag.Units = db.EquipmentLists
    .Where(s => s.Unit != null)
    .OrderBy(s => s.Unit)
    .Select(s => new SelectListItem
    {
        Value = s.Unit + " - " + s.MakeModel,
        Text = s.Unit + " - " + s.MakeModel,
    });

and adjust the view to

@Html.DropDownListFor(m => m.Unit, (IEnumerable<SelectListItem>)ViewBag.Units, "Select one...", new { @class = "form-control" })
  • Thanks for the explanation on the arguments for selectlist. Really hit home. New to MVC and it's difficult to understand the tips VS is spewing out when creating a constructor. – user2673097 Nov 30 '17 at 21:57
  • I would also suggest that you use a view model and get rid of your use of `ViewBag` ([What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc)), and your view model will contain a property `IEnumerable Units` - refer the code in [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for a typical example –  Nov 30 '17 at 21:59
0

What Stephan said is correct, but let me explain it in a little detail.

ViewBag.[VAR] = new SelectList([list], [value], [text]);

uses the SelectList constructor detailed here

SelectList(IEnumerable items, string dataValueField, string dataTextField);

As long as you have a collection that inherits IEnumerable, it'll work with a select list. dataValueField refers to whatever you want the html value property to have, dataTextField refers to whatever you want it to display.

This is handy to use with a data table object as you could just concatenate fields as the text. For example:

public class Obj1
{
   public int id {get; set}
   public string field1 {get; set;}
   public string field2 {get; set;}
   public string DisplayField {get {return field1 + " " + field2;}}
}
//In your controller
public ActionResult Create()
{
   ...other code
   List<Obj1> list = GetAllObj1(); //however you get the fields
   ViewBag.Fields = new SelectList(list, "id", "DisplayField");
   return View();
} ...

Hope this helps some.

Memedon
  • 907
  • 2
  • 7
  • 14
0

Please use below code in Controller side ViewBag.Units = db.EquipmentLists.Select(s => new SelectListItem{ Value = s.Unit +" - " + s.MakeModel,Text = s.Unit + " - " + s.MakeModel});

and Use Below Line in View Side @Html.DropDownListFor(m => m.Unit, (IEnumerable)ViewBag.Units,"Select one...", new { @class = "form-control" })