locked
Ideas on making Table Readonly (-Edit, -Update actions) RRS feed

  • Question

  • User-217289338 posted

    I am looking for ideas to make a table readonly allowing only List action.

    I have a table Employees and would like to make sure that the route is not possible for actions other than "List".
    I want to allow 'http://localhost/site/Employees/List.aspx
    but remove or some how not allow 'http://localhost/site/Employees/Edit.aspx.

    I need to keep the last route in the global.aspx for "{table}/{action}.aspx" for other tables.

    I know I could remove any hyperlinks or redirects, but I want to make sure the url is disabled also.

     

    Thank You

    Eric Brasher

    Monday, August 18, 2008 5:38 PM

Answers

  • User660823006 posted

    Today internally Dynamic Data set the IsReadyOnly to true when we detect a database view. So if you create a view in SQL and pull it into the Linq to SQL designer, then Dynamic Data will treat it as read only.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 20, 2008 11:59 AM

All replies

  • User933528419 posted

    One idea is to create an additional page template (let's say ReadOnly.aspx) based on List.aspx that doesn't have any of edit/insert/delete functionalities and create an additional route like the following:

    routes.Add(new DynamicDataRoute("Employees/{action}.aspx")
            {
              Action="List",
              ViewName= "ReadOnly",
              Table = "Employees",
              Model = model
            });

    There might be something easier to do just with the routes.

    I hope this helps.

    Maíra

    Monday, August 18, 2008 9:28 PM
  • User-797310475 posted

    Here's one solution you could use in your Global.asax:  

    Type[] readOnlyTableTypes = new Type[]{
        typeof(Product), typeof(Category) // declare other types here
    };
    var readOnlyTables = model.Tables.Where(t => readOnlyTableTypes.Contains(t.EntityType)).ToList();
    string readOnlyTablesConstraint = String.Join("|", readOnlyTables.Select(t => t.Name).ToArray());
    var otherTables = model.Tables.Where(t => !readOnlyTables.Contains(t));
    string otherTablesConstraint = String.Join("|", otherTables.Select(t => t.Name).ToArray());
    
    routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
        Constraints = new RouteValueDictionary(new { action = "List", table = readOnlyTablesConstraint }),
        Model = model
    });
    
    routes.Add(new DynamicDataRoute("{table}/{action}.aspx") {
        Constraints = new RouteValueDictionary(new { action = "List|Details|Edit|Insert", table = otherTablesConstraint }),
        Model = model
    });

     Basically, your first declare a route for all of the read-only tables that only supports the "List" action. Then, you declare a route for all the other tables that supports all actions. The important thing to note here is that you cannot have a generic DynamicDataRoute at the very end because of the way route resolution works: it will evaluate all routes until it finds a match. If you kept the generic route, it would catch all incoming requests even though the earlier routes seem to exclude them.

     

     

    Monday, August 18, 2008 9:32 PM
  • User-797310475 posted

    Another thing to note is that you will have to handle disabling the Delete button seperately, as that has nothing to do with routing. Because of this it might be easier to also have your read-only route point to a modified page template, as per Maira's suggestion.

    Monday, August 18, 2008 9:36 PM
  • User-217289338 posted

    Maíra,

    Thanks for the solution, this looks like it will work. After creating the readonly.aspx, then adding the Employees table route, 'http://localhost/employees/edit.aspx now forwards to the readonly view which works great.

     

    Thanks

     Eric

    Wednesday, August 20, 2008 9:08 AM
  • User-217289338 posted

    I just found some logic that seems to be using the metatable.isreadonly property. In the PageTemplates.List.aspx Page_Load there is a statement that drops specific crud commands from the gridview.

    // Disable various options if the table is readonly
    if (table.IsReadOnly)
    {
    GridView1.Columns[0].Visible =
    false;
    InsertHyperLink.Visible =
    false;
    }

     How can I get the IsReadOnly set to true using the linq to sql model?

     Thanks

    Eric

    Wednesday, August 20, 2008 9:45 AM
  • User660823006 posted

    Today internally Dynamic Data set the IsReadyOnly to true when we detect a database view. So if you create a view in SQL and pull it into the Linq to SQL designer, then Dynamic Data will treat it as read only.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 20, 2008 11:59 AM