This is what I’m trying to accomplish.
Basically I have Currency model which contains ID,Name, BuyValue, MidValue, SellValue, and DateCreated.
Now I want to use them in converter I’ve come up with. It has two drop down lists, where first ddl is From which currency is converted and second To which currency it is converter. Regular stuff.
The first ddl should always contain Name as text in ddl and BuyValue as value, whereas second should always contain Name as text and SellValue as value. Something like this in simple HTML terms.
<select id="ddlFirst">
<option value="BuyValue1">Name1</option>
<option value="BuyValue2">Name2</option>
...
</select>
<select id="ddlSecond">
<option value="SellValue1">Name1</option>
<option value="SellValue2">Name2</option>
...
</select>
Of course this isn’t the code for it, it’s just clarifying the explanation above.
I’m quite a beginner on this matter, and literally have no clue where to start. What gets me the most is should I somehow separate those values into different models and create a view model out of them or could I just use them somehow. To be honest I’m quite confused so any help is appreciated.
UPDATE
var currencies = db.Currencies.Where(c => c.DateCreated.Equals(today));
var list = currencies.Select(s => new { s.ID, s.Name, s.BuyValue, s.SellValue }).ToList();
var model = new ConverterViewModel
{
FromValues = list.Select( x => new SelectListItem { Value = x.BuyValue.ToString(), Text = x.Name}),
ToValues = list.Select(x => new SelectListItem { Value = x.SellValue.ToString(), Text = x.Name }),
};
This comes up empty though. 🙁 And I know it’s not.
Yes, of course, you should define a view model:
and then have a controller action populate this view model and pass it to the view:
and finally have a strongly typed view: