I want to use 2 models. The first is on Index.cshtml
page, and the second is on _Layout.cshtml
page
In the controller which contains the action public ActionResult Index(){...}
, I declare some values and return it to View(). Like this:
public ActionResult Index()
{
HomePageViewModel model = new HomePageViewModel();
// do something...
return View(model);
}
And in MyProjectName.Models
, I write some classes to check login account and put it on the page _Layout.cshtml
. Like this:
On page _Layout.cshtml
:
@using MyProjectName.Models
@model MyProjectName.Models.LoginModel
@if (Model.LoginAccount != null)
{
foreach(Account acc in Model.LoginAccount)
{
@Html.ActionLink(@acc.Email, "SomeAction", "SomeController", null, new { id = "loginEmail" })
@Html.ActionLink("Logout", "SomeAction", "SomeController", null, new { id = "logout" })
}
}
The code on page _Layout.cshtml
doesn't work. It said that: I have returned a model (HomePageViewModel model
), but some values which I want to render is referenced from MyProjectName.Models.LoginModel
Main requirement is: the first model is used to display the content on page Index.cshtml
, and the second model is used to check user login (on page _Layout.cshtml
).
Can you tell me how to do that? Thank you!