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.
Related
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.
ViewData & ViewBag serve the same purpose of transferring data from Controller to View or between Views.
The difference between them is the underlying implementation and the way we need to handle it as a result.(casting in case of ViewData etc.)
So, can there be any scenario where ViewData is preferred over ViewBag?
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
Being a wrapper it holds no data itself - it's just a shortcut for accessing ViewData. You have no reason to use the ViewData directly nowdays.
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)
I have a menu on a shared view and on click of menu item i have to set a value in session or any object that i need on other view page. I am using MVC architecture usging razor. Please help with your answer. Thanx..
You could store the object in the session in the controller action rendering the view. This way when you click on the menu link and get redirected to another controller action you will be able to fetch this object that was previously stored from the session.
You can use Session at razor view by writing session within code block.
e.g.
#{
Session["uname"] = "somevalue";
}
but there are some more options available in mvc, that you should try viewdata, viewbag and tempdata.
Can we Get those Values which is in Session directly in asp.net MVC Views?
I have tried it in Controller it Works,I can easily get & set the Value,
But how to Get value directly from Session in Views?
You can, but you shouldn't as this is considered as bad practice. Views are not supposed to pull data. They should use data passed from the controller. So in your controller read the session and then pass the value to the view for rendering. This rule can be easily broken by using the Session property of the View page:
<%= this.Session["someKey"] %>