Editing
Web Template
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Using DataModels=== The Razor engine provides to each template page a parameter called <code>Model</code>, by which data can be retrieved. By default, it contains no data except for a table called "User", which contains the current user. To load the required data for the page, a call to <code>Model.LoadModel(string[] requiredTables, params IDataModelQueryDef[] requiredQueries)</code> must be executed. The first parameter, <code>requiredTables</code>, specifies which tables should be loaded. By default, <code>LoadModel</code> loads nothing. In general, the CompanyInformation, CompanyLogo and the current entity are useful to load, so a call could look like: <code>Model.LoadModel(new string[] {"CompanyInformation", "CompanyLogo", "Employee"})</code>. The second parameter specifies a way to narrow the load of the data, preventing unnecessary data from being loaded. For example, if one only requires the ID and Name fields of an employee, one might call:<pre> Model.LoadModel( new string[] {"CompanyInformation", "CompanyLogo", "Employee"}, new DataModelQueryDef<Employee>( null, new Columns<Employee>(x => x.ID).Add(x => x.Name), null) ) </pre> By default, all columns in the database are loaded, so this is a way to drastically reduce the amount of data loaded. (The other two fields, set to <code>null</code>, specify a filter and sort order for the retrieved data.) ====Retrieving Data==== To get a table that has been loaded, use <code>Model.GetTable<T>(alias)</code>. This returns an object of type <code>CoreTable</code>, which has a property <code>Rows</code>, through which one can retrieve data. An example is shown here.<pre> Model.LoadModel(new string[] {"Employee"}); // Load Model CoreTable employeeTable = Model.GetTable<Employee>(); // Get Table from Model // Iterate through the rows foreach(CoreRow row in employeeTable.Rows){ // Convert the row to an Employee. This is not necessary, and the data can also be retrieved with row[ColumnName] or row.ToDictionary()[ColumnName] var employee = row.ToObject<Employee>(); <p>@employee.ID: @employee.Name</p> } </pre>
Summary:
Please note that all contributions to PRS may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
PRS:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
Edit source
View history
More
Refresh
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information