Rob Angelier
Rob Angelier

Reputation: 2333

How to manage view management inside a asp.net mvc 3 application

I'm not sure if this is the right place for my question, but i will give it a try.

We are starting the development of a new product wich will use asp.net mvc 3 razor as the presentation layer. The application has about 7-8 different roles and all the roles have different views on wich the data will be displayed. I've build a nice framework with the help of NHibernate and the implementation of a couple design patterns, so this side of the applicatie will be easy to adjust and apply maintenance to.

I want the presentation layer to be as adaptive as my framework and that's why i'm trying to figure out a way to have as less views as possible for all the different kind of roles but with full functionality. What kind of implementation do you use for distributing views to the end-user based on his role?

I thought about using jquery templates, but with the current possibility of views and the razor syntax it sound a bit unnecessary and not the preffered way to go.

Any information that i should keep in mind while developing this application will be nice, think about, common mistakes, best practises and suggestions etc.

Looking forward to your replies.

Upvotes: 1

Views: 379

Answers (1)

E. Barnes
E. Barnes

Reputation: 179

One way to reduce the number of views would be to use IsInRole() (or an equivalent if you have something custom) in combination with partial views. In the most simple case, you could have one view that also renders partial views within it based on specific roles. Something along the lines of:

@if (HttpContext.Current.User.IsInRole("admin")) {
    @Html.Partial("_StuffForAdmins")
}

Depending of your situation, some partial views could then be shared across multiple different roles, reducing duplicate code. User interface elements that are publicly accessible by all roles could just be placed on the view itself without role checking.

EDIT: An alternative syntax is:

 <div>
        @if (Context.User.IsInRole("admin"))
        {
            @Html.Partial("_StuffForAdmins")
        }
    </div>

Upvotes: 2

Related Questions