c# - How to use more than 1 Model on a page by using asp.net mvc 5? -
i want use 2 models. first on index.cshtml
page, , second on _layout.cshtml
page
in controller contains action public actionresult index(){...}
, declare values , return view(). this:
public actionresult index() { homepageviewmodel model = new homepageviewmodel(); // something... return view(model); }
and in myprojectname.models
, write classes check login account , put on page _layout.cshtml
. 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. said that: have returned model (homepageviewmodel model
), values want render referenced myprojectname.models.loginmodel
main requirement is: first model used display content on page index.cshtml
, , second model used check user login (on page _layout.cshtml
).
can tell me how that? thank you!
in layout use html.action()
or html.renderaction()
call childactiononly
method returns partial view loginmodel
[childactiononly] public actionresult login() { loginmodel model = // initialize model want display in layout return partialview(model); }
and create partial view displays links, in layout
@ { html.renderaction("login", "yourcontrollername") }
Comments
Post a Comment