我有这样的课程
public class class1
{
public int class1id { get; set; }
public string name { get; set; }
}
public class class2
{
public int id { get; set; }
public int class1id { get; set; }
public string from { get; set; }
public string to { get; set; }
}
public class viewmodel
{
public class2 deletedocument { get; set; }
public IList<class2> deletelist { get; set; }
}
现在我想返回一个名为from的列表到属性。这里来自class1和class1id的名称是外键(class1)。我怎样才能做到这一点?我需要改变我的视频模型吗?
答案 0 :(得分:3)
<强>模型强>
您可以做的是使Model
具有这三个属性:
public class MyModel {
public string name { get; set; }
public string from { get; set; }
public string to { get; set; }
}
查看强>
然后在Razor
View
中,您宣布IEnumerable<MyModel>
为Model
@model IEnumerable<ProjectName.Models.MyModel>
<强>控制器强>
在您的Controller
中,您从其他两个查询中进行查询,在流程中创建IEnumerable<MyModel>
并将其返回
public ActionResult Index(){
var models = from a in blabla //query what you want here, generate IEnumerable<MyModel>
join b in blabla2 on a.class1id equals b.class1id
select new MyModel{ name = a.name, from = b.from, to = c.to}
return View(models);
}