Can I use the global MVC filter to disable form fields?

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

Some ideas so far:

  • Some combination of a global action filter and editing templates.
  • Html custom helper, something like Html.SecureTextBox etc.

I tend to number 1, but I'm wondering if any of you guys / girls with extensive MVC experience have decided to solve this problem.

+4
source share
2 answers

I agree with the use of a base view model, or perhaps just with an interface like CanEdit. If you are walking through the interface, you can set the property in ActionFilter in the OnActionExecuted method.

To bind it to a view, creating a new HtmlHelper would be pretty simple. I would use TextBoxFor as the base class, since it has access to the view model. Then you can check the property and create the necessary HTML attribute. However, after going through this route, you will need to create a new helper for each type of input control you need (text box, selection list, etc.).

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

Several other people mentioned that server-side restriction is still necessary so that people do not bypass client restrictions. To do this, you will need an action filter. This link has a good idea about this.

+1
source

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

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

0
source

All Articles