Reputation: 1031
I have two models and I need to display data in my layout page and in every page that the user visit. Those two models have not any relationship between them so I don't need any join.
this is my controller
public ActionResult Index()
{
var notification = (from n in db.Notification
where n.NotificationIsSeen == true
select n);
var task = (from t in db.Task
where t.TaskIsSeen == true
select t);
return View();// I not sure how to return both of queries
}
I also create a model that contains both of them but I 'not sure if this is the right way
public class Layout
{
public Notification Notification { get; set; }
public Task Task { get; set; }
}
and in my layout page
@model IEnumerable<MyprojectName.Models.Layout>
//other code
@foreach (var item in Model)
{
<li>@Html.DisplayFor(modelItem => item.Notification.NotificationSubject ) </li>}
//other code
@foreach (var item in Model)
{
<li>@Html.DisplayFor(modelItem => item.Task.TaskSubject )
</li>
}
I have seen other similar question but they work with join tables. I need some help on returning data of both tables. thank you in advance
Upvotes: 0
Views: 655
Reputation: 1
Using partial view for build the dynamic header
1 - create action with partial view and display data
2 - go to layout to call this
@Html.partial("Action","Controller")
Upvotes: -1
Reputation: 506
Please declare list type of model in you layout model
Layout Model
public class Layout
{
public IEnumerable<Notification> Notifications { get; set; }
public IEnumerable<Task> Tasks { get; set; }
}
Controller
public ActionResult Index()
{
Layout model = new Layout();
model.Notifications = (from n in db.Notification
where n.NotificationIsSeen == true
select n);
model.Tasks = (from t in db.Task
where t.TaskIsSeen == true
select t);
return View(model);
}
View
@model MyprojectName.Models.Layout
@foreach(var item in Model.Notifications)
{
// access your item.propertyname
}
@foreach(var item in Model.Task)
{
// access your item.propertyname
}
Upvotes: -1
Reputation: 26028
Your queries in your action method both return collections of data. To accommodate this your view model needs to have two lists and needs to look something like this. You have to be able to store these collections in lists when sending them to the view:
public class Layout
{
public IEnumerable<Notification> Notifications { get; set; }
public IEnumerable<Task> Tasks { get; set; }
}
To populate these lists change the code in your action method to this. Create an instance of Layout
, populate the two lists and then send the instance to the view:
public ActionResult Index()
{
Layout model = new Layout();
model.Notifications = (from n in db.Notification
where n.NotificationIsSeen == true
select n);
model.Tasks = (from t in db.Task
where t.TaskIsSeen == true
select t);
return View(model);
}
Your view needs to accept and instance of Layout
:
@model MyprojectName.Models.Layout
@foreach (var notification in Model.Notifications)
{
<div>
@notification.NotificationSubject
</div>
}
@foreach (var task in Model.Tasks)
{
<div>
@task.TaskSubject
</div>
}
I hope this helps.
Upvotes: 3