0

I have a FK in my Details ViewModel and when the View binds to the ViewModel I only get the FK back (expected). The FK is a reference to a simple ID/Name type table. I also have a Strongly typed List in the VM representing that FK-referenced table. I want to do something like

<div class="display-field">@Model.ManufacturersList.Find(x => x.ID == Model.softwaremanufacturerid))</div>

While this will return the the instance I want...I can't figure out how to get the "Name" attribute to display.

Sorry if this is more a Lamda question but thought I'd try all the same

Thanks

Bayrat
  • 161
  • 1
  • 3
  • 17

1 Answers1

0

If .ManufacturersList.Find(x => x.ID == Model.softwaremanufacturerid) returns what you want, don't do it in the View. The View should only display data, while the model layer should really be doing the searching (.Find)

In your view model, add a string property for ManufacturerName

public string ManufacturerName { get; set; }

In your controller,

MyViewModel vm = new MyViewModel()
{
    ManufacturerName = .ManufacturersList
        .Find(x => x.ID == theFKAlreadyInTheViewModel)
};
return View(vm);

Then in your view,

@Model.ManufacturerName

OR, more simply, you could use the ViewBag

ViewBag.ManufacturerName = YourManufacturersList
    .Find(x => x.ID == theFKAlreadyInTheViewModel);

Then in your View,

@ViewBag.ManufacturerName
David Fox
  • 10,603
  • 9
  • 50
  • 80
  • Your way is more elegant and keeps in in the VM...I got to work by simply adding ".name" to the end of the nasty lamda I did – Bayrat Mar 15 '11 at 18:33
  • I've found in a lot of web apps sticking to a 1:1 View:ViewModel ratio can be tedious, but worth it when you need to start refactoring. It's not always the case, though. – David Fox Mar 15 '11 at 18:35