Asp.net core 2.1 - Layout controller - asp.net-mvc

I'm learning asp.net core 2.1.
I made a database with Menu table ( ID, Name, Actio,Controller)
And i wanted to pass data from database to menu.
To do that i need a controller who will pass data to view. How to pass these data to _Layout.cshtml? I will need it for child (partial view "Menu").
In templete mvc there is no controller for Layout...

In the _layout.cshtml you could simply have a
#Html.Action("Method", "Controller")
in the body which you can use to call another controller. Ideally if this is a navbar etc I'd return a partialView from this method.

I managed to do it by using ViewComponent
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-components?view=aspnetcore-2.1
#Html.Action("Method", "Controller") Doesn't work in Asp.Net CORE 2.1

In Core 2.1 You need to use
<partial name="PartialViewName"/> instead of #Html.Action("PartialViewName","ControllerName")
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-2.2 See here.

Related

Asp.net MVC way to pass data to _layout.cshtml?

I keep reading that the MVC way to pass data from a controller to the view is done via a ViewModel, but what about passing data to _Layout.cshtml, like page title, meta description, article author, etc...
What's the MVC way to pass this kind of data? Should I just use ViewBag for them?
You have few ways:
ViewBag
ViewData
View Components
Injections (example below)
ViewBag and ViewData are quite easy to use, however not always convenient.
There is one big plus - you could set/read them in one place of view and read in another - for example, you could set them in your main view and read/display them in _lauout.cshtml.
View Components are the most interesting new feature in MVC Core (in my opinion) which allows you to create UI widgets.
There is a little bit more coding for ViewComponent (you need to create controller and view), but it's flexible feature (I like it) and easy to call in a place where you need it, just
#await Component.InvokeAsync("NameOfCOmponent").
Injections not my favorite, but sometime usfull - for example if you want display user name, you could just put the following code directly into your layout/view file:
#using System.Security.Claims
#using Microsoft.AspNetCore.Identity
#inject UserManager<ApplicationUser> userManager
#{
var userInfo = ((await userManager?.GetUserAsync(User))?.xxx);
// where 'xxx' is any property of ApplicationUser model
}
then you can use #userInfo in the same view to display that info.
More information:
Views Overview
Passing Data to Views
View Components

how to keep value of viewBag asp.net mvc in view

i have 7 viewBags which holds data in view when the Page Reload (switch to another language ) the ViewBag value became empty
how to keep values of these viewBags after Page reload
i'm using asp.net mvc 5
ViewBag is just for passing from controller to view. You would want to use Session state if you want to persist data on post backs.

Using MVC Partial View's own model instead of its parent's

I need to populate a list box in a partial view, using ASP.NET MVC4.
Can Partial View have its own #model, as opposed to taking a model from its parent as described here?
I can populate my dropdown box using a separate AJAX call to another MVC controller (i.e. not parent page/url) as discussed here, but the resulting syntax is a bit more clumsy; furthermore, an additional endpoint is exposed to the outside world.
Yes - you can call partial view with any model you like. There is no requirement that data somehow comes from current model.
#Html.Partial("PartialView1", new MyOtherModel(42))

Passing data from controller superclass to view?

I'm writing an Asp.net MVC application (my first MVC app). I need to pass data to the _Layout view to customize the header and footer on my pages - which user is logged in, if they have any notifications, etc. The _Layout page always needs this information, but the child pages do not.
How should I pass this data to the view? Can I create a LoggedInUser property that the view can access, in the same way there is a Model and ViewBag? LoggedInUser could be populated by the base controller class.
Or is there a better way to implement this?
If you're using MembershipProvider and/or RoleProvider you can do as webdeveloper pointed out to get the identity of the current user User.Identity.Name, if showing it's name is what you want.
Also you could type your _layout to use a specific model, but I don't recommend it. See this question's answer for further details.
Lastly you could populate a ViewBag property on your controllers to have the user information you need.
I wanted to point out that you could do a partial view to achieve this, and avoid _layout typing and populating the ViewBag on each request.
To detect authentication:
User.Identity.IsAuthenticated
Then you can use sections ASP.NET MVC 3: Layouts and Sections with Razor or/and #Html.RenderAction (Html.RenderAction and Html.Action)

Populating the model for a shared view, embedding a shared view within another view

I'm trying to embed a small view snippet that steps through a model fragment that works fine when I embed it in a single controller and pass it to a view like so;
Controller:
return View(_entities.formTemplate.ToList());
View:
http://www.pastie.org/666366
The thing is that I want to be able to embed this particular select box in more than just this single action / view, from the googling I've been doing this appears that it should go into a shared view, but I'm not clear then on how I could populate the model within that view from the controller? (or maybe I'm completely missing the purpose for shared views?)
In the other MVC framework I'm accustomed to working with there is the concept of a filter where you can call code before or after any action and mod the model as it passes the controller and goes to the view, is such a thing possible in .net mvc?
Any assistance appreciated.
You'll want to use the HtmlHelper method DropDownList() in order to create a input:
<%= Html.DropDownList("id", new SelectList(formBuilder, "ID", "Name")) %>
You probably want to use a ViewUserControl here.
You have a couple of options if you go that route. If it's model data that is easily available, recreate it at the call site of your RenderPartial like so:
<%=Html.RenderPartial("ViewName", new ModelData())%>
If it's data that is dependent on the current model data, then you'll need to pass that data somehow to your partial view.
ASP.Net MVC also has the concept of before/after controller actions. You decorate your controller method with an Attribute that derives from ActionFilterAttribute. In there, you have access to OnActionExecuting and OnActionExecuted.

Resources