开发者

ActionResult Details needs to resolve FK from List to display Name?

开发者 https://www.devze.com 2023-02-17 04:08 出处:网络
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

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 g开发者_运维问答et the "Name" attribute to display.

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

Thanks


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
0

精彩评论

暂无评论...
验证码 换一张
取 消