user controls and asp.net mvc - asp.net

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.

Related

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.

Multiple asp.net UI Projects in one solution to share master page

I have searched under different keywords but am not sure if I have found one that is exactly what I'm looking for, even though I have to think it's been asked before. We have say 4 seperate asp.net projects today that we would like to combine into one master solution to share the same master page/ header control. We would have a tab control in the master page, each tab would point to a seperate project. We would need to pass information between projects. An example would be an account list selection from project 1 would need to be retained for project 2 when the user clicks on the project 2 tab. I have to think there is a way to implement this outside of something like iframes. Appreciate any thoughts/insight in advance. Thanks!
Sounds like a good candidate for MVC (model-view-controller). Multiple projects can be loaded into one solution, and shared. If you want to eliminate duplicate views, you could use Razor to render partial or shared views, or a layout page. Ref http://www.asp.net/mvc for more info.
You mentioned iframes. If you're looking for a synchronized solution, you could try multiple views per controller. For an asynchronous solution, AJAX through jQuery etc could work (http://api.jquery.com/category/ajax/).

Having a UI layer and presentation layer

Let's say I'm on a list page and I
page to, say, page 10. Then I select
a record on that page and redirect to detail
page. After that, I click on the edit
to redirect to the edit page.
After I update the record I'm redirected back to
the detail page. I, then, press back
to go back list to continue my browsing from
where I left off. The key here is
where I left off in the list which is
page 10.
What is the best way to handle this?
Initially, I put a hidden field called page number in each of the webforms and pass it along with the querystring back and forth. Seemed like a lot or a bit redundant checking the querystring on each page and passing it.
I was wondering if there are some other ways. for instance, I've been reading about a separation between the UI and the presentation layer is a good idea (for larger scale apps). To me I understand it as all click handler events will yield control over to the presentation layer which is just a plain class?
Is this correct? Also, is the presentation layer suppose to implement something particular? I know this could probably be saved in session but could someone humor me and show me how to use a presentation layer to handle this (I know it would be overkill but is it possible?)
I don't think there is THE best way. Everything depends of what you achieve to do, ie. the requirements of the whole project.
After all, according to the description, I don't even understand why are you having three pages to do a single thing. By the way, ASP.NET data controls handle mostly everything for you, so you don't even have to ask yourself how to do this (except if you have serious reasons to avoid ASP.NET controls).
For example, a simple <asp:ListView /> will let you list items page per page and show details when a single item is selected. Edition of an element is also quite easy.
What you are asking for is well... large and could span multiple blog posts to give a complete understanding of UI Design Patterns.
I have a small example of MVP with Asp.Net here: What is the best way to reuse pages from one website in another?
However, it is not exhaustive. If you really want information on this you should do some looking into a framework such as WebForms MVP, or ASP.Net MVC.
Check out ASP.NET MVC. It is a framework which goes on top of ASP.NET to do the separation between the presentation layer and business layer.
For simplicity, what you are describing is a very good example of the perfect place to use Asp.Net Dynamic Data.
It's incredibly easy and powerful, and easy to modify once you dig into it a bit. I'd start with the videos here: http://www.asp.net/dynamicdata
I've been using this more and more on every project, for at least the simple CRUD portion of it. I really can't express how much I love this tool now that I'm used to it.

Does someone have real project experience using asp.net mvc implement complicated web UI app?

In my past projects I've been using webform to implement windows form style complicated backend admin page by using asp.net webform.
Just wondering by using asp.net mvc, can it make this kind of complicated UI page much easier?
This highly depends on what UI you need. For me, it wouldn't be easy without jQuery... but with jQuery it's very convenient.
For example of possible nesting, my app has cart that contains Accordion for customers, with each pane containing custom tabs with sliding animations for orders, with each order containing table of items, where each item can be expanded to show included products details (not to mention context menus to change customers, on-the-fly discounts changing with notification tooltips, popups to show product details, and so on). All this on single page. And I still find it very easy to manage, because all the functionality is well split across MVC controllers and views.
As for "admin part", I use two-level nesting (with second level on another page, but I just don't want to go deep into jqGrid), and it's 5 minutes to setup a new admin page using AutoMapper, custom attributes, and some custom code. Got new entity (e.g. SomeProduct)? 5 minutes and new admin page with grid, custom editing controls and formats is done. With MVC.
But if you need traditional "data grid" approach, chances that ASP.NET will do better. Or maybe jqGrid (or third-party grids like Telerik) will help you, because they have subgrids and all this stuff. I would say, if you don't like Domain-Driven Design, if your application is not object/entity driven, but highly based on raw data tables, then MVC might be not what you need. But I may be wrong because I never worked with such applications. All other apps, I'd prefer MVC.

ASP.Net MVC - Showing Model data based on Roles

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.

Resources