Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Values in SelectList text

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).

like image 622
Zeb-ur-Rehman Avatar asked Jan 25 '13 19:01

Zeb-ur-Rehman


2 Answers

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.

like image 195
David L Avatar answered Nov 08 '22 14:11

David L


I am not sure whether this will help. In a c# point of view, you can structure your class like

public class Parts
{
    public string PartCode { get; set; }

    public string PartName { get; set; }

    public string PartNo
    {
        get
        {
            return PartCode + PartName;
        }
        set;
    }
}

and then use the select list like.

SelectList(Parts, "PartNo")
like image 4
Muthukumar Avatar answered Nov 08 '22 16:11

Muthukumar