Multiple Values in SelectList text

asp.net-mvc-3, c#

Solution

According to microsoft documentation, SelectList(IEnumerable, String, String) Initializes a new instance of the SelectList class by using the specified items for the list, the data value field, and the data text field.

http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist%28v=vs.108%29.aspx

In order to do this, you would want to concatenate the name into a new property on your model. The below stack overflow answer with 8 ups is my favorite way to do this.

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

In your case, it would look something like this...

public class Model
{
    public string pcode { get; set; }
    public string pname { get; set; }
    public string ConcatDescription { get; set; }

    public string ConcatDescription 
    {
        get 
        {
            return string.Format("{0} {1}", pcode , pname );
        }
    }
}

Then you can just call that property directly.

SelectList(Parts, "pno", "ConcatDescription ")

EDIT: Added in code for clarification.

Problem

I am trying to show multiple values in `dropdownlist` text. The example is given following. ``` SelectList(Parts, "pno", "pcode"+"pname") ``` where `pno` is Part number. `pcode` is part code. `pname` is part name Now please tell me how can i concatenate part code and name with out using viewmodel(I want to do it using domain model).

Original source

Related problems