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).
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With