Tuesday, 27 August 2013

Columns from two models in one webgrid

Columns from two models in one webgrid

first, please note that I am a little new in that. I would like to know,
how can I make Webgrid with columns from two models (two tables).
I have two models, like that: Model1:
public int Id { get; set; }
public string Name { get; set; }
Model2:
public int Id { get; set; }
public int Model1_Id { get; set; }
public string Level { get; set; }
public string AdditionalInfo { get; set; }
public string Note { get; set; }
In controller handle data and send to view.
...
List<Model2> data = new List<Model2>();
...
return View(data)
Now starts the problem. In the view I am creating Webgrid from the model.
Once problem is, that names of columns have to be identical with property
names or it falls.
@model IEnumerable<Model2>
<div class="class-name">
@grid.GetHtml(columns: new [] {
grid.Column("Level"),
grid.Column("AdditionalInfo", header: "Additional info"),
grid.Column("Note")
}, tableStyle: "some-name")
</div>
That's all ok, but I want in that grid also name from Model1 and the
values will be depends on Model1_Id.
If I try add new column by grid.Column it falls.
If I add second model (after the first line) it writes 'Only one 'model'
statement is allowed in file'.
Everything what I tried it fell.
I tried work with google but after several hours, I would like ask you
guys. Thank you

No comments:

Post a Comment