I've just recently started to build a web site using ASP.NET - MVC 4, and Im trying to wrap my head around how this works.
There are some data I would like to display on every page and have understood that Partial Views are great for this.
Is it possible to create a controller that always provides data for the partial view? Or how can I solve this?
That's called a child action.
Call Html.Action(...).
You can create a controller action for the partial view. But if you are looking for inlcluding some thing on every page, you should think about adding that to your _Layout.cshtml page
You can create a normal action method which returns a partial view like this
public ActionResult UserInfo()
{
UserViewModel objVm=GetUserInf();
// do some stuff
return View("PartialUserInfo",objVM);
}
This will return a view with name "PartialUserInfo" present in your Views/Users folder( Assuming your controller name is Users. If you want to specify a view which is a different location you can mention it when calling the View method
returnView("Partial/UserInfo",objVm);
This will return a View called "UserInfo" in your Views/Users/Partial folder.
in your partial view, you can disable the normal layout( if you have one) by doint this
@model UserViewModel
@{
Layout=null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With