How can I combine two fields in a SelectList text description?

mvc dropdownlistfor multiple columns
dropdownlist in mvc 5 razor example
asp net core dropdownlist
html dropdownlist concatenate values
mvc create selectlist from list
ienumerable<selectlistitem dropdownlistfor
mvc populate multiple dropdown list
mvc populate dropdownlist from viewmodel

I want put in a selected list labels the name and surname of people of an EF model. I've tried with this:

public ActionResult Insert()
        {
            ViewData["accountlist"] = new SelectList(time.Anagrafica_Dipendente.ToList(), "ID_Dipendente", "Surname Name", null);             
            Giustificativi g = new Giustificativi();
            return View(g);
        }

but VS returns an error, because there isn't a attribute called "surname name". how can i concat the name and surname in the selectlist label?

thanks

you could do something like this:

ViewData["accountlist"] = 
    new SelectList((from s in time.Anagrafica_Dipendente.ToList() select new { 
        ID_Dipendente=s.ID_Dipendente,
        FullName = s.Surname + " " + s.Name}), 
        "ID_Dipendente", 
        "FullName", 
        null);

MVC SelectList combining multiple columns in text field, How would I generate a select list, where the text field, is made up of two or more text columns, eg: Where I have a Description and Rate field in  The Format of the constructor that you are using is SelectList(IEnumerable items, string dataValueField, string dataTextField). So when you use it the way you have you are actually telling it to bind to the TextField called "Description-- £Rate" and if this is not what the field is called coming in the from the DB it won't know what you are indicating.

Add a new property to time.Anagrafica_Dipendente which will represent the concatenation of the two properties:

public string Fullname 
{
    get 
    {
        return string.Format("{0} {1}", Surname, Name);
    }
}

and then use this:

ViewData["accountlist"] = new SelectList(
    time.Anagrafica_Dipendente.ToList(), 
    "ID_Dipendente", 
    "Fullname", 
    null
); 

Update: As of C# 6.0, the property can be more concisely written as:

public string Fullname => string.Format("{0} {1}", Surname, Name);

Learn more about expression-bodied properties here.

[Solved] Mvc, var product = new ProductService().ProductByClubView(clubname.Trim()).ToList​(); var result product = db.Product .Where(x => x.clubname  I am returning some stored contacts to view for DropDownList and I am not able to include multiple dataTextFields on my SelectList. Currently I have: @Html.DropDownListFor(model =&gt; model.Account.

I was looking for this answer and my easy way is adding a NotMapped attribute, this is my code:

[NotMapped]
public string FullName
   {
     get
       {
         return Name + " " + LastName;
       }
   }

Then you can use the normal way in the controller, this is my code, Owner the the foreign key related to AspNetUsers

ViewBag.Owner = new SelectList(db.AspNetUsers, "Id", "FullName", product.Owner);

asp.net, So when you use it the way you have you are actually telling it to bind to the TextField called "Description-- Rate" and if this is not what the field is called coming in  The Format of the constructor that you are using is SelectList(IEnumerable items, string dataValueField, string dataTextField). So when you use it the way you have you are actually telling it to bind to the TextField called "Description-- £Rate" and if this is not what the field is called coming in the from the DB it won't know what you are indicating.

How to combine multiple text fields in SelectList in ASP.NET MVC , I have a SelectList in my project that shows only one text field, "First-name" from database. Need to combine "Middle-name" and "Last-Name"  Problem with ASP.Net MVC SelectLIst and List<SelectListItems> There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'xxx' How can I combine two fields in a SelectList text description? How can I get this ASP.NET MVC SelectList to work?

Mastering Visual Basic .NET Database Programming, It is also possible to concatenate two fields in the SELECT list with the value to text with the CONVERT() function before you can concatenate the two with the +  You can also concatenate text from multiple cells using the ampersand (&) operator. For example, you can enter =B2&" "&A2 to get the same result as =CONCATENATE(B2,” “,A2) . There’s no real advantage of using one over the other. although using the ampersand operator results in a shorter entry.

Adobe Dreamweaver CS5: Complete, Adobe Dreamweaver C55 Quick Reference Summary (continued) DW 124 to form Merge cells in table Drag image from Assets panel or Files panel Alt text Click Forms tab on Insert bar, click Select (List/ Menu) button List Item button in  From your description, it seems that you want to display multiple columns in DropDownList. If that is the case, I suggest you could use LINQ to get the required format of your model and then send it to the view.

Comments
  • This technique also allows combining properties from different linked objects. eg FullName = s.Parent.Surname + s.Name
  • In order for that to work, Fullname would have to be a member of time.Anagrafica_Dipendente.
  • i can't believe i couldn't think of that my self, simple and elegant, love it!
  • Or even more succinctly using public string Fullname => $"{Surname} {Name}";