session managment in mvc - asp.net-mvc

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.

Related

How to pass data from controller to Master page in asp.net MVC?

I can't use ViewData in master page, and I think it's not a smart way to use ContentPlaceHolder control. For example, I want to transfer a string to master page, how should I do?
Could you give an example?
Use the TempData property to pass data from controller to masterpage. The TempData property value is stored in session state. The value of TempData persists until it is read or until the session times out. If you want pass data one controller view to another controller view then you should use TempData.
public ActionResult NewCustomer()
{
TempData["SomeValue"] = "";
return View();
}

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)

Can we Get the Session Values in Views in 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"] %>

Pass data to Master Page with ASP.NET MVC

I have a hybrid ASP.NET WebForms/MVC project. In my Master Page, I have a "menu" User Control and a "footer" User Control. Anyways. I need to pass some data (2 strings) to my "menu" User Control on my Master Page (to select the current tab in my menu navigation, etc.) My views are strongly-typed to my data model. How can I push data from my controller to my menu or at least allow my master page to access some data pre-defined in my controller?
Note: I understand this violates pure ASP.NET MVC, but like I said, it is a hybrid project. The main purpose of my introduction to ASP.NET MVC into my project was to have more control over my UI for certain situations only.
Put your strings into the ViewData collection,
ViewData["MenuString1"] = "My First String";
ViewData["MenuString2"] = "My Second String";
and retrieve them in the Master Page like this:
myMenu.Property1 = ViewData["MenuString1"].ToString();
myMenu.Property2 = ViewData["MenuString2"].ToString();
http://nerddinnerbook.s3.amazonaws.com/Part6.htm
You can use ViewData to share data between the Controller and View that is not in the model. In the Controller, do something like ViewData["menu"] = myMenu; and then in the View do <%= ViewData["menu"] %>. You can pass objects but you need to cast the ViewData[key] back to the object type in the View.
Another way to do this is to put your menus and other non-Model related data needs into a separate controller. Then you can use RenderAction to call the action in your navigation controller that generates the menu. Hack has a blog post on RenderAction that explains this in more detail.
I lean towards using ViewData for temporary values from the controller like select lists and a RenderAction for unrelated things such as the main site navigation.

MVC 2 RenderAction

Im having a value in ViewData, lets say htmlhelper.ViewData["myData"]="some";
And in partial page I can overwrite the myData's value.
But when I using the Html.RenderAction() and call a partial page.
In the same partial page htmlhelper.ViewData["myData"] is null.
When you call RenderAction, you create an entirely new ViewData instance for your partial page. If you want ViewData["myData"] to be visible by your other action, either pass it to the subaction or put it in TempData.
I figured out from MVC source code. Cool that we have MVC as open source.
htmlHelper.ViewContext.HttpContext.Items["myData"]
this will maintain the value from Partial and RenderAction case.

Resources