user2315795
user2315795

Reputation: 43

Render strongly type partial view in layout MVC4 razor

I have some issues with rendering strongly type partial view in layout view.

  1. partial view (menu) is band with model Menu
  2. this partial view I want to render in _layout.chtml, so it 'll be avliable in all views.

I want to make avalible the partial view(menu) in all pages/view. the problem I face is where to put the action for partial view to populate it from DB on page load.

thanks

---------------------- My code is--------------------
partial view inside shared folder.
@model List<Menu>
@foreach(var item in Model){// here is the html/model item inside to display}
--------------------------------------
HomeView.chtml inside home folder

@model List<homemodel>
.... here goes html code/ plus homemodel loop/data etc.
------------------------------
HomeController{
public ActionResult HomeView()
{
.........return view();
}
public PartialViewResult partialmenu()
{
// data from db
return partialview(partialobject as list);
}

------------------------
layoutview.chtml

--html code---
{@ Html.renderpartial("partialview");}
.. html code...

Upvotes: 0

Views: 2866

Answers (2)

Olrac
Olrac

Reputation: 1537

where to put the action for partial view to populate it from DB on page load.

every view has it's own controller..regardless if it is partial or not...therefore you could populate your view on it's own controller...

Upvotes: 0

webdeveloper
webdeveloper

Reputation: 17288

I want to make available the partial view(menu) in all pages/view. the problem I face is where to put the action for partial view to populate it from DB on page load.

Write in layout: Html.RenderAction('MyMenu') OR Html.Action('MyMenu') and then populate it from any source. Your action will return strongly typed model.

Upvotes: 0

Related Questions