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.
Note: That this route must appear before the default routes.
Happy coding and remember your a PC but for us coders I prefer the short lived “Live to Code and Code to Live”
Important: The only caveat here is that the Delete button still works. :( But you could catch that in Business logic if you didn’t want to edit each page.
8 comments:
Hi,
I Used [ReadOnly(true)] in my table, but it doesn't work for foreign keys! Which attributes should I use for make them read only also?
Best regards,
Sahar
Hi Sahar, you need this article here (http://csharpbits.notaclue.net/2009/01/making-field-read-only-via.html)
Steve :D
make sure you have the right name for the column, since it's a foreign key VS might have capitalized it and appended it with the number 1.
:)))
That is understood :) and has been cover several time in the forums plus you would get an error when you run your appp as the meta model woudl not recognise the table.
Steve
Awesome,
My solution is not exactly the same as yours but it is fairly similar.
Thanks,
Tahir
Hi Tahir, this is a fairly old post and I would not do this this way today.
Steve
Steve, could you please give me a clue to how do that?
I already installed your Github packages, but i´m in the beginning! :(
Thanks, from Portugal =)
Now you just add the [ReadOnly(true)] to the table and the rest is done for you.
Post a Comment