Can we Get the Session Values in Views in asp.net MVC? - asp.net-mvc

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"] %>

Related

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 ASP MVC 6, how do I get a variable set in every page template?

I have some extra information about a user that I need to have in every page template (not just the layout). How can get this information, which is a property on my base controller, into every every template?
I've read about making a WebViewPage and setting that as the base view in web.config, but MVC 6 doesn't use a web.config file.
You must to override the OnActionExecuting method in your base controller, and set the viewbag data in method body.
ViewBag.UserName = yourObj.UserName;
ViewBag.UserId = yourObj.UserId;
In your template, you just get this data by ViewBag.
For real project with a lot of users use DonutCaching library. So you can render only the template for the user and the rest of the page take from cache. It makes your server faster.

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)

how to access querystring in ASP.Net MVC View?

How do I access the querystring value in a View?
It is not a good design to access query parameters in a view. The view should use the model provided by the controller. So the controller reads query parameters and passes them to the view. If you want to ignore this rule you could always do this in your view:
<%= Request["SomeParameter"] %>
But I would strongly discourage you from doing so.
In View, you can access it directly. No need to write any code in Controller, though you can.
For example -
If your querystring has parameter named id, something like ?id=1
Razor syntax:
#Request.QueryString["id"]
In .Net Core:
#Context.Request.Query["SomeParameter"]
I would read the querystring value in your Controller, and then set that value to a property in your ViewBag. The ViewBag property can then be read in from your view.
e.g:
ViewBag.MyQSVal = Request.QueryString["myValue"];
Then, in your View:
#if(ViewBag.MyQSVal == "something"){ ... }
As Darin suggested you should not use Querystring in view. But one thing is you can access Request variable in your view because its Asp.Net and if you access it you have all the functions and member that are present there

How to find out the current resource and controller name from the view in ASP.NET MVC

I'm using the ASP.NET MVC in a RESTful resource manner and I want to be able get the current resource and controller name from the view.
I'm attempt to create a HTML page and I want to know the current resource and controller name is it possible to get this?
you can get that in a view by looking at the ViewContext.Controller property. The ViewContext property also gives access to many other useful properties such as Route Data, Application, Cache, ViewData, etc.
EDIT: To get the actual name of the controller you can go one of two ways:
1) Call GetType() on the Controller property of ViewContext and use that Name property to get the class name of the controller
2) Look at the route data and examine the values for the "controller" key, e.g. ViewContext.RouteData.Values["controller"] (this would likely be the preferred method)
This information is available in the ViewContext property of ViewPage (assuming you are using .aspx for your views).
You can always run GetType while in the controller (or just type it in if thats what you want to do) and store it in the view data. If you are using the same view from multiple controllers, you might want to make it a strongly typed view and have the controller name be part of it.
Can you be more specific with why exactly you want to do this? There might be a better way.

Resources