I have 2 controllers that generate 2 index views. What i would like to do is use these views as global shared partial views but cant seem to get this working.
Does anyone know if this is even possible?
My controller code is
public ActionResult Index()
{
var viewModel = (from P in db.Projects
join R in db.Reports on P.ProjectTitle equals R.ReportProjectID into ps
from R in ps.DefaultIfEmpty()
select new MyViewModel { Project = P, Report = R });
return View(viewModel);
}
My ViewModel code is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MiLife2.ViewModels
{
public class MyViewModel
{
public Project Project { get; set; }
public Report Report { get; set; }
}
}
and my view is
@model IQueryable<MiLife2.ViewModels.MyViewModel>
@{
ViewBag.Title = "Index";
}
enter code here
<h2>Index</h2>
<div>@Html.Partial("_Partial1")</div>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Project.ProjectTitle </td>
<td>@item.Project.ProjectCreatedByID</td>
<td>@item.Project.ProjectCreatedDate</td>
<td>@if (item.Report == null)
{
<text>No Reports</text>
}
else
{
@item.Report.Title;
}
</td>
<td>@if (item.Report == null)
{
<text> </text>
}
else
{
@item.Report.Description;
}</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Project.ProjectID }) |
@Html.ActionLink("Details", "Details", new { id=item.Project.ProjectID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Project.ProjectID })
</td>
</tr>
}
</table>
If i create a partial page and paste the above view into it and then use @HTML.Partial("_ProjPartial") i get the error
The model item passed into the dictionary is of type 'System.Collections.Generic.List1[MiLife2.Project]', but this dictionary requires a model item of type 'System.Linq.IQueryable
1[MiLife2.ViewModels.MyViewModel]'.
This does not happen if i use @HTML.Partial("_ProjPartial") from within the Index cshtml page in the specific controller views folder.