Reputation: 99
I have classes like this
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; }
}
Now i want to return a list with name,from,to properties. Here name from class1 and class1id is foreign key(class1). How can i do this? Do i need to change my viemodel?
Upvotes: 2
Views: 1596
Reputation: 30813
Model
What you can do is to make a Model
having those three properties:
public class MyModel {
public string name { get; set; }
public string from { get; set; }
public string to { get; set; }
}
View
And then in your Razor
View
, you declare an IEnumerable<MyModel>
as your Model
@model IEnumerable<ProjectName.Models.MyModel>
Controller
And in your Controller
you query from your two other queries, creating IEnumerable<MyModel>
in the process and return it
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);
}
Upvotes: 3