views:

38

answers:

2

Some users of our application will have read-only access to many of our pages, in our current web forms app this means they see the form, but all of the fields are disabled. We're looking at MVC 3 and searching for the cleanest, most idiomatic way of implementing this functionality.

Some ideas so far:

  1. Some combination of a global action filter and edit templates.
  2. A custom Html helper, something like Html.SecureTextBox etc...

I'm leaning towards number 1, but I'm wondering if any of you guys/gals with more MVC experience have solved this problem in a better way.

A: 

My preference would be to set a variable in a common base view model (or ViewData), using a global action filter, and then use a bit of jquery to dynamically disable the input fields, delete buttons etc.

$(':input').attr('readonly', true);

Clicktricity
All a user has to do is disable javascript to edit fields they don't have permissions to edit?
jfar
good point, perhaps the reverse. Draw everything disabled, and then use jquery to enable it.
Clicktricity
Disabling the form fields is really just a convenience for the user, back-end code will prevent any changes from happening if they shouldn't.
Dave
A: 

I agree with using a base view model, or perhaps just an interface with a "CanEdit" type of property. If you go the interface route, you could set the property in an ActionFilter in the OnActionExecuted method.

To tie it to the view, creating a new HtmlHelper would be pretty easy. I'd use TextBoxFor as the base class, since it has access to the view's model. You can then inspect the property and create the necessary HTML attribute. However, with going this route you will need to create a new helper for each type of input control you need (textbox, select list, etc).

Without knowing all the details of what you are doing, a much simpler idea would be to not provide a Save button for read-only users. The Save button would be driven by one property in the view model (or ViewData, if you like).

Several other people mentioned that a server-side restriction is still needed to prevent people from bypassing the client-restrictions. You will need an action filter for this. This link has a good idea about that.

Jason Capriotti