views:

29

answers:

1

Hi,

I am new to MVC and LINQ.

I have a table with three columns (id, text1, text2). What I want to do is generate a selectList in my view which has DataValue set to table.id, and DataText set to be = text1 + ", " + text2; (i.e. join text1 and text2 with comma separation).

What do you reckon is the best way to achieve this?

Cheers,

Tim.

A: 

Something like this perhaps?

var v = dc.threecolumntable.Select(x => new {DataValue = x.id, DataText = x.text1 + ", " + x.text2 })
Frank Tzanabetis
Thanks for the reply. I may have confused myself there. By DataValue I think I meant "key", as in " SelectList(list, "Key", "Value", "SelectedValue"); ". And I possibly don't understand this dropdown list properly. So you have a relational table (id 1 = value x, id 2 = value y, etc). You want a dropdown list which shows values x, y, etc; but when selected and passed to the controller POST method, goes to update the model (i.e. the other object/sql table) with the ID. But in this case the text to display is the combination of the two strings...Just to confirm before I try your answer...
nulliusinverba
Well, inside the curly braces is what's known as an anonymous type, so you can name each property whatever you like.You can change it to this instead - new {Key = x.id, Value = x.text1 + ", " + x.text2 }
Frank Tzanabetis
Thanks, mate - I see where you're going with that. I will mark you the answer.
nulliusinverba