ASP.Net MVC - Showing Model data based on Roles - asp.net

I have a View which needs to show and hide details based on the users role. I have 2 options
using an inline if statement in the View to show and hide details
Create multiple partial views and use to controller to detect the role and then load the appropriate Partial view.
Im a newbie to MVC so can someone please advise what the best way is for approaching this problem.

If it is something related to how the information displayed on the screen (and it sounds like it is) then it is best to keep that in the view. Personally I would use partial views and only load them when needed, this supports better reuse.

I'd probably do different views for every role. I've found that over time the views for each role diverge in "common" content.

Related

How to query data and display them in the common layout page (_layout.cshtml)?

I had tried the tutorial https://learn.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-3.1&tabs=visual-studio
And I am able to display the data with Razor pages with page model.
However, when I was trying to find ways to display a student name at the top using the common layout page, Shared_layout.cshtml, I got lost and would hope to seek your advice on how I could query for the data from the Student Entity and display just one name in the common layout page.
I felt lost because there is no page model for the common layout page.
Note: I am new to application development, so hope you could provide some guidance to enlighten me. Any good learning reference would be great as well :)
You can use ViewBag to display student name at the top.
Add ViewBag in all your action methods where you are returning view as return type from controller files.
ViewBag.StudentName = "Yiwen";
Use it in your common layout file.
#ViewBag.StudentName
You can see microsoft link on how to use ViewBag.

Making a Menu for each user Role in .NET Web Forms

I have been doing typical menu from code behind where i just show and hide divs based on their role but code becomes very long cause i have to hide every single div of each role for each user.
My question is... Is there a better practice to do this? I also just found out about Login View, is this the way to go in Web Forms?
Your code behind should not take the responsibility of altering how the View is displayed, that's the job of the view (and that's why asp controls exist).
One approach would be to have a logic that builds up a List and this list is passed as DataSource of an asp control.
Actually there's a good control that allows you to iterate a collection of elements defining a display template for each element in that list, here you can find the details:
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater?view=netframework-4.7.2
I hope this sets you in the right path.
A common way to do this is in a WebForms app is to use a SiteMap.
A site map can be linked to roles, so that only those elements that are accessible for a user's roles are displayed. And recent versions can be made to display reasonable cleanly, as divs that can be styled how you want (the original version generated a lot if inline styles).
For an MVC sitemap there are similar solutions, such as this one.

Sending a model for use throughout all views linked from a layout

I am working on a web service built on ASP.NET MVC 4 and have run into some trouble figuring out how to manage a model after log in so that it is widely available to all action functions created in the back end.
For example) Let's say I have built a service that allows a user to log in and manage multiple databases. After he logs in he is redirected to a view that allows him to pick which database he would like to manage, which inherits the old layout. After he clicks on the database he wants to manage, the model of this database is passed to the index view of a manage controller which utilizes a different layout than before. All of the managing action links are coded in the layout but the model was passed to the index page. He needs the model to be available for all the ActionResults in the managing controller, but a model can't be passed to a layout, so this is where I'm stuck.
Does anyone know of any good references or guides on how I can utilize a model throughout a whole layout? Or something a long these lines? I'm not sure the technique I used for this problem was the best but I don't know how else to go about this. If anyone knows a better technique of going about this please let me know.
Thank you!

Is it possible for two or more Drupal Views Page displays to share the same path?

I would like to have a user land on a page and see on view that depends on her role. When I tried to create two views with the same path, Views did not object. But I'm wondering if it's not meant to work this way.
Is it two views or two displays on the same view? According to the maintainer of the Views module, two displays on the same view are meant to be able to have the same path, but two different views should not be able to have the same path.
If the results of the view are based on the user's role then I would recommend using one view/display but use the user's role(s) as an argument.
If you have a small number of roles that don't change very often you could create individual displays and adjust the display's 'Access' settings in the view for each one. I would not consider this a best practice but there might be certain situations where this is fits the bill.

user controls and asp.net mvc

Here is one trivial question, that I am not sure how to handle.
I need to display list of categories on every page, and to be able to choose items from a specific category to be displayed. I use asp.net MVC, and have chosen to create a user control that will display categories. My question is: what is the best approach to pass data to a user control. I already found some information in these blog posts:
http://weblogs.asp.net/stephenwalther/archive/2008/08/12/asp-net-mvc-tip-31-passing-data-to-master-pages-and-user-controls.aspx
http://blog.matthidinger.com/2008/02/21/ASPNETMVCUserControlsStartToFinish.aspx
I would like also to hear your opinion.
PS. I'd like to hear Jeff's opinion, especially because of his experience with UC's on Stackoverflow
I'm using mvc components, which replaced ascx user controls in preview 4.
Example:
http://blog.wekeroad.com/blog/asp-net-mvc-preview-4-componentcontroller-is-now-renderaction/
So, you call components action from View, which then choose View to render. You can pass data in this call also.
it is the mvc futures project. i will probably try this
http://forums.asp.net/t/1303328.aspx. I need to render menu with categories.

Resources