locked
Registering Multiple Models RRS feed

  • Question

  • User-330204900 posted

    Is it possible to multiple Models registered and then show each model through a different default page for example:

    • Default.aspx
    • Permissions.aspx

    Or would you need two different websites?

    Thursday, June 12, 2008 7:08 PM

Answers

  • User-2063762995 posted

    Right now the "default.aspx" grabs all models at once and lists the tables within them interlaced with one another.  The default master table was never intended for deployment (as it is rather ugly) but a way to see that everything is working and as a placeholder.  Our hope is that users would replace the intro page with something custom with explicit declarations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 7:20 PM
  • User1641955678 posted

    Note that there are two different things you can do:

    1. Register multiple contexts on the same MetaModel.  They then all share the same routes.
    2. Create multiple MetaModel (each with its own set of contexts).  They can then use different routes.

    And from any MetaTable, you can get at its MetaModel (via the Model property). 

    David

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 8:00 PM
  • User1641955678 posted

    Default.aspx by default will only show the first registered model.  This happens because the code behind has:

        System.Collections.IList visibleTables = MetaModel.Default.VisibleTables;

    To access other models, you can do one of two things:

    • Simply hold on to it in a static variable after you create it (typically in global.asax), and make it available to the whole app.
    • You can get it from a context type by calling MetaModel.GetModel(typeif(YourContext)).

    David

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 17, 2008 2:52 PM

All replies

  • User-2063762995 posted

    Right now the "default.aspx" grabs all models at once and lists the tables within them interlaced with one another.  The default master table was never intended for deployment (as it is rather ugly) but a way to see that everything is working and as a placeholder.  Our hope is that users would replace the intro page with something custom with explicit declarations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 7:20 PM
  • User-330204900 posted

    Thanks Erik, thats great [:D]

    Is there a way of telling which model the table is from, like an entry in the metadata?

    Thursday, June 12, 2008 7:36 PM
  • User1641955678 posted

    Note that there are two different things you can do:

    1. Register multiple contexts on the same MetaModel.  They then all share the same routes.
    2. Create multiple MetaModel (each with its own set of contexts).  They can then use different routes.

    And from any MetaTable, you can get at its MetaModel (via the Model property). 

    David

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 12, 2008 8:00 PM
  • User-330204900 posted

    Thanks David that just about covert it [:D]

    Thursday, June 12, 2008 8:22 PM
  • User-330204900 posted

    Hi David if you follow option two will the default.aspx page show both sets of tables and if not how would you access the second MetaModel to for instance show a seperate list of tables.

    Tuesday, June 17, 2008 12:49 PM
  • User1641955678 posted

    Default.aspx by default will only show the first registered model.  This happens because the code behind has:

        System.Collections.IList visibleTables = MetaModel.Default.VisibleTables;

    To access other models, you can do one of two things:

    • Simply hold on to it in a static variable after you create it (typically in global.asax), and make it available to the whole app.
    • You can get it from a context type by calling MetaModel.GetModel(typeif(YourContext)).

    David

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 17, 2008 2:52 PM
  • User-330204900 posted

    Thanks David that answers it.

    Tuesday, June 17, 2008 3:19 PM
  • User-330204900 posted

    Hi Davis this is what I've done:

    Added a duplicate GridView called Menu2

    // Show Admin Table is user in Admin
    String[] roles = Roles.GetRolesForUser();
    var attributeModel = (MetaModel.GetModel(typeof(AttributesDataContext)));
    var visibleTables2 = attributeModel.VisibleTables;
    if (visibleTables2.Count > 0 && roles.Contains("Admin"))
    {
    	Menu2.DataSource = visibleTables2;
    	Menu2.DataBind();
    	Menu2.Visible = true;
    }
    

    This works I get the other set of tables if the user is a member of the Admin role.

    Neat [:D]

    Tuesday, June 17, 2008 3:37 PM
  • User630118385 posted

    The answer is probably alread in this post but beyond my ability to comprehend, so sorry if this is a stupid question...

    What would I have to enter in the content area of Default.aspx (or elsewhere) in order to menu my tables thus:

    Main Menu

    * Table1

    * Table 2

    Lookup Tables

    * Lookup Table 1

    * Lookup Table 2

    How do the hyperlinks to Dynamic Data maintenance screens read when you make them in a regular sitemap rather than the dynamic Gridview?

     

    Thanks

    Tuesday, October 7, 2008 2:23 PM
  • User-330204900 posted

    Hi Norisk, What I would do is find some way of differentiating which table are which say a new attribute.

    and then I would do something like this:

    Default.aspx.cs 

    public partial class _Default : System.Web.UI.Page
    {
    	protected void Page_Load(object sender, EventArgs e)
    	{
    		System.Collections.IList visibleTables = MetaModel.Default.VisibleTables;
    		if (visibleTables.Count == 0)
    		{
    			throw new InvalidOperationException("There are no accessible tables. Make sure that at least one data model is registered in Global.asax and scaffolding is enabled or implement custom pages.");
    		}
    
    		// adding your own table to the list
    		var tablesMain = from t in MetaModel.Default.VisibleTables
    						 where t.Attributes.OfType<LookupAttribute><LOOKUPATTRIBUTE>().SingleOrDefault() == null 
    						 select new MenuItem
    						 {
    							 ListActionPath = t.ListActionPath,
    							 DisplayName = t.DisplayName
    						 };
    
    		Menu1.DataSource = tablesMain;
    		Menu1.DataBind();
    
    		// adding your own table to the list
    		var tablesLookup = from t in MetaModel.Default.VisibleTables
    						   where t.Attributes.OfType<LookupAttribute><LOOKUPATTRIBUTE>().SingleOrDefault() != null
    						   select new MenuItem
    						   {
    							   ListActionPath = t.ListActionPath,
    							   DisplayName = t.DisplayName
    						   };
    
    		Menu2.DataSource = tablesLookup;
    		Menu2.DataBind();
    	}
    
    	public class MenuItem
    	{
    		public String ListActionPath { get; set; }
    		public String DisplayName { get; set; }
    	}
    }
    

    Defaul.aspx

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    	<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server" />
    	<h2>My Tables</h2>
    	<br />
    	<br />
    	<asp:GridView ID="Menu1" runat="server" AutoGenerateColumns="false" CssClass="gridview"
    		AlternatingRowStyle-CssClass="even">
    		<Columns>
    			<asp:TemplateField HeaderText="Table Name" SortExpression="TableName">
    				<ItemTemplate>
    					<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("ListActionPath") %>'><%#Eval("DisplayName") %></asp:HyperLink>
    				</ItemTemplate>
    			</asp:TemplateField>
    		</Columns>
    	</asp:GridView>
    	<br />
    	<br />
    	<h2>My Lookup Tables</h2>
    	<br />
    	<br />
    	<asp:GridView ID="Menu2" runat="server" AutoGenerateColumns="false" CssClass="gridview"
    		AlternatingRowStyle-CssClass="even">
    		<Columns>
    			<asp:TemplateField HeaderText="Table Name" SortExpression="TableName">
    				<ItemTemplate>
    					<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#Eval("ListActionPath") %>'><%#Eval("DisplayName") %></asp:HyperLink>
    				</ItemTemplate>
    			</asp:TemplateField>
    		</Columns>
    	</asp:GridView>
    </asp:Content>
    
    Atrtribute: 
    [AttributeUsage(AttributeTargets.Class)]
    public class LookupAttribute : Attribute
    {
    	public Boolean LookupTable { get; private set; }
    
    	public LookupAttribute(Boolean lookupTable)
    	{
    		LookupTable = lookupTable;
    	}
    }
    Metadata: 
    [Lookup(true)]
    public partial class Category
    {
    }
    
    [Lookup(true)]
    public partial class Employee 
    {
    }
    Hope this helps [:D]
    Tuesday, October 7, 2008 3:16 PM
  • User-330204900 posted

    How do the hyperlinks to Dynamic Data maintenance screens read when you make them in a regular sitemap rather than the dynamic Gridview?

    Hi Norisk, I'm not sure what you mean?

    Do you mean binding the sitemap control to the visibleTables variable?

    Tuesday, October 7, 2008 3:57 PM
  • User630118385 posted

    >>>Do you mean binding the sitemap control to the visibleTables variable?

    I mean, could I manually create hyperlink that does this:

     "DoDynamicDataMagicForm("MyTable)"

     Or menu a few of them:

     "DoDynamicDataMagicForm("MyTable)"

     "DoDynamicDataMagicForm("MyOtherTable)"

    Or do I HAVE TO have a dynamically built menu based on criteria?

    One reason to avoid the dynamic menu of tables is that they are presented alphabetically. I want to make my own order.

    Tuesday, October 7, 2008 4:07 PM
  • User-330204900 posted

    No you can manually generate the links or you can do it in code the url format for linking to the table is:

    http://localhost:20246/DD_NW/Suppliers/List.aspx

    where

    http://localhost:20246/DD_NW/{table}/Action.aspx

    Hope thsi helps [:D]

    Tuesday, October 7, 2008 5:06 PM
  • User630118385 posted

    Yes, that helps a lot.

     

    Tuesday, October 7, 2008 7:45 PM
  • User630118385 posted

    Ok, I noticed that my status bar showed the link I needed in my environment. I didn't want to hard code the server and port, so I wound up with this link:

     /MyWeb/MyTable/list.aspx

    Thanks for your help.

    Friday, October 10, 2008 8:30 PM
  • User-330204900 posted

     /MyWeb/MyTable/list.aspx

    Your best bet would be to set your links like so

    ~/MyTable/list.aspx

    ~/MyTable/Edit.aspx

    ~/MyTable1/list.aspx

    ~/MyTable1/Details.aspx

    etc.

    Saturday, October 11, 2008 2:46 AM