Use viewBag in Master page Asp.net MVC - asp.net-mvc

I am developing a website using asp.net MVC
I want to show the name of user after he/she logged in the website.
The problem is that User.Identity.Name returns user email and I do not want to change that because user name is not unique. And I want to access email in other pages.
I like to use a viewBag in my master page, but I do not know where to define it.
If I define it in Home/controller, it works just for this action.
Should I use filter?

This link will help you with the problem of passing data to master page.
This is the "official" way to handle that, it explains in a very clear way, but I strongly recommend to read the first link completly too.

If you're using MVC, then I would strongly suggest moving to a ViewModel pattern to accomplish this, rather than stuffing objects into the ViewBag, mostly for two reasons: 1) Type Safety, and 2) Intellisense support.
In this kind of situation, you would have your MasterPage inherit from a BaseViewModel, and your actions would return derived objects from the BaseViewModel. You can then set data on your view model, and it will be available to the master page when rendering:
<%# Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MyViewModelBase>" %>
Alternatively, you could make a 'partial view' that renders just the content you need based on the user's state.

Related

Is there a way to pass value from one page to another in MVC

Is there any other way to pass value from one view to another or one controller to another apart from the methods below
Viewdata
TempData
Viewbag
or passing inline in page redirect code?
Any sort of communication between the views, excluding nested views i.e. partials, should go via the controller. That's kinda the whole point of MVC.
It sounds to me like you are sort of trying to get around the M/C part of the framework therefore it leads me to question is it the right tool for you to be using.

Maintaining input control state in MVC

In the app I'm working on the administrator has the ability to select a customer to work with in a drop down list. Thing is that there are a large number of views that the administrator can go through and each time they'd have to select the customer again. In webforms this was rather easy... store it in a Session variable and reset it when another page loads.
MVC not so much. I seem to be stuck at the point where I pass this value to the view from the controller. We are storing the value in a Session variable which we access using a base controller like this:
MyController.CurrentUser.CurrentCustomerId
The question I can't solve is how I pass this value to a partial view. The customer selector tool is in a partial view which is added to pages that need it.
I thought of using the ViewBag, but that means that in every single action in my controllers that requires this value I would have to add:
ViewBag.CurrentCustomerId = CurrentUser.CurrentCustomerId;
And even then I'm not sure if the ViewBag data is carried through to the partial view. I think it is.
Seems like there should be a more efficient way to do this and still abide by MVC rules?
#Andre Calil provided a very good solution for carrying user information between controllers and views and I ended up using this approach.
See his answer at the following link: Razor MVC, where to put global variables that's accessible across master page, partiview and view?

Umbraco MVC Implementation - View does not depend on Umbraco implementations

I have followed this nice tutorial which explains how you can take even more advantage of using Umbraco as a content delivery system. Tutorial MVC Umbraco => Your model and views should not depend on specific Umbraco implementations which is a huge advantage for real front-end developers.
The controller inherited from Umbraco.Web.Mvc.RenderMvcController in order to access the data from the CMS.
Now my problem is that we can't use #HTML.actionlink for navigation, it seems that this is only supported in SurfaceController.
Now my question is how would we implement navigation inside Umbraco.Web.Mvc.RenderMvcController? Can we still use the native #HTML.actionlink tag?
No you can't. Simply because all requests pass through a single action. In order to retrieve a path to a CMS-managed page, you need to use the node/content traversal the #Model provides. See here for more details on this.
Edit
To clarify, the author of the article is suggesting that the Umbraco implementation should be more in line with a traditional MVC implementation with little or no logic in the views. Therefore, any querying of node data should happen prior to the view (e.g. in the Mappers). So this is where you would have to retrieve the links.
Umbraco's default MVC implementation forces all requests to go via a single action on a single controller. The author's implementation allows the requests to be shared across one controller per document type - which is better IMO. But it still means that things like Html.ActionLink are redundant in the views since there isn't an action per page.
Further edit
If you wanted to build a navigation list with a combination of Umbraco-managed pages and non-Umbraco pages, regardless of the implementation, I would:
Create a child action and view for the navigation in a separate NavigationController that inherits from the SurfaceController
Use the this.CurrentPage property of the inherited SurfaceController to traverse the Umbraco content cache to retrieve the relevant Umbraco-managed pages. You can then use the Url property of each page result to get its path, and the Name property to get the page title
Use this.Url.Action("action", "controller") to retrieve the paths to specific non-Umbraco actions. Alternatively, if the pages are database-managed, use you data layer (e.g. EF, NHibernate, PetaPoco) at this point
Combine these in a Dictionary to make the list you require where the Key is the path and the Value is the page title
Pass this down to the view as the view model.
Of course there any many more things to consider like caching, but in a nutshell, that's a fairly basic implementation.

How do I generate content once and reuse it across all pages in ASP.NET MVC?

In my ASP.NET MVC site all pages share the same content (header & top menu).
What are my options to generate that content is only one place and reuse it in all pages. I also want the link the user is currently on to be handled appropriately (not show up as a link).
In MVC3 this is usually done in the layout page: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx
In my ASP.NET MVC 2 application, I use a Master Page as suggested by Rich. I just wanted to add that I use a Master Model that all of my view models inherit from to return dynamic data that is needed in the master page (i.e. something to help you determine the active link to highlight). Every time I render a view I explicitly pass it a view model so those values from the master model are always available.
That master model gets its data from things that are globally available (i.e. my custom identity, http context properties, etc.) - because I don't want to manually initialize those properties every time I render a view (which defeats my goal of thin controllers).
You can also use ASP.NET Master pages. http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
If you start a new ASP MVC project in Visual Studio I believe it'll give you a masterpage in /Views/Shared
If it's dynamic data; then you can always use a RenderAction() inside of a Masterpage. Is this dynamically generated data?

How do I display multiple pieces of user information on every page in ASP.NET MVC 2?

I'm building a site in ASP.NET MVC 2 that will allow for anonymous users and registered users. When a user is logged in, I want to display multiple pieces of information related to that profile on every page (i.e. hometown, favorite color, etc.). From a view perspective, I understand using Master pages and creating partials to keep it DRY.
However, where I am getting stuck is how do I pass this user information to the view for every page? I already have the relationships between database tables established (I'm using EF), so I can do this on an individual basis for each action through ViewData, but that's obviously ridiculous for every page on the site.
So far, my research has started to lead me down the path of creating a base controller and base view model that the other controllers and view models will inherit from. But I feel like I'm missing something obvious. Any pointers?
If you have your Master page use the RenderAction method, it can invoke controller actions for the various repetitive parts of your page, each of which can perform data access and render a partial view. That allows you to separate your view models while still displaying certain elements on all your pages.
This approach works great for us.
We use a base controller to store it in ViewData.
You could also use an action attribute on the controller rather than inheriting from a base controller.
You could create a base class for your models that contains the data that is display on every page.

Resources