我有两张桌子。人与城市。人员表有CitiID。城市有CityID和CityName。我只想从表城市中获取城市名称。他们之间有一个外键。我找不到必要的代码。 (可能这么简单的问题,但我现在真的无法弄清楚,很难找到答案,因为它的关键词非常普遍)
public ActionResult Index (int id)
{
var personInfo = db.Person.FirstOrDefault(x => x.PersonID == id);
var person = new PersonVM(); // This is my model view.
person.Name = personInfo.Name; // OK
person.Surname = personInfo.Surname; // OK
person.CityName = ??? // personInfo.Cities.???
return View(person);
}
答案 0 :(得分:0)
哇。我找到了两个解决方案我以前找不到它们因为它们比我想的那么容易:
var person = new PersonVM(){CityName = personInfo.Cities.CityName};
或
person.CityName = personInfo.Cities.CityName