These are two thing you can do to make your table Read Only.
- Add an attribute for instance the ReadOnlyAttribute or your own custom attribute
- Using Routing to restrict access to the Edit or Insert pages
The first solution I covered in this article Writing Attributes and Extension Methods for Dynamic Data so I’ll just show the Routing example here.
// Setting a table to Read only var visibleTables = from t in MetaModel.Default.Tables where t.Scaffold == true select t; var readOnlyTables = new StringBuilder(); foreach (var table in visibleTables) { var isReadOnly = table.Attributes.OfType<ReadOnlyAttribute>().
DefaultIfEmpty(new ReadOnlyAttribute(false)).
FirstOrDefault();
if (isReadOnly.IsReadOnly) readOnlyTables.Append(table.Name + "|"); } routes.Add(new DynamicDataRoute("{table}/{action}.aspx") { Constraints = new RouteValueDictionary(new { action = "Edit|Insert", table = readOnlyTables.ToString().Substring(0, readOnlyTables.Length - 1) }), ViewName = "Details", Model = model });
Listing 1 – Routing example.
[ReadOnly(true)] public partial class Employee {}
Listing 2 – sample attribute.
In Listing 1 we see three things going on:
- We get a list of visible tables using the Scaffold attribute to find out if the table should be shown.
- Next we loop through the visible tables and if the table is set to ReadOnly then we add it to the readOnlyTables StringBuilder to build a list of read only tables (e.g. “Products|Customers”).
- Build a route that constrains the these tables to only get to the Details page.
Happy coding and remember your a PC but for us coders I prefer the short lived “Live to Code and Code to Live”