简体   繁体   中英

asp.net mvc selectlist “{ }” appears in result

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.

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 (eg 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" })

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM