Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controller for partial views ASP.NET MVC

Tags:

asp.net-mvc

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?

like image 203
johan Avatar asked May 10 '26 08:05

johan


2 Answers

That's called a child action.

Call Html.Action(...).

like image 66
SLaks Avatar answered May 11 '26 21:05

SLaks


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;
}
like image 39
Shyju Avatar answered May 11 '26 21:05

Shyju