In a tutorial posted by Mike, Clickable webGrid Row, once a row was clicked, it will be redirected into another page displaying the data related to the clicked row. At the foreach section, column names from the database were dynamically displayed. what
if I want to remove some columns that I don't want to be displayed? How about renaming some of the columns? Thank you for responses!
@foreach(var item in data.GetDynamicMemberNames()){
<strong>@item:</strong> @data[item]<br />
}
If you want to do this modification for all the detail pages, the easiest way is changing the SELECT statement. The details.cshtml would become, e.g.:
@{
Page.Title = "Details";
var db = Database.Open("Northwind");
// var query = "SELECT * FROM Customers WHERE CustomerID = @0";
var query = "SELECT CustomerID AS Id, CompanyName AS Company, ContactName AS Contact, " +
"Address, City, PostalCode AS [Postal Code] FROM Customers WHERE CustomerID = @0";
var data = db.QuerySingle(query, UrlData[0]);
}
<h1>Details</h1>
@foreach(var item in data.GetDynamicMemberNames()){
<strong>@item:</strong> @data[item]<br />
}
@{
var sql = @"SELECT CompanyName AS [Company Name],
ContactName AS [Contact Name]
FROM Customers WHERE CustomerId = 'ALFKI'";
var data = Database.Open("Northwind").QuerySingle(sql);
}
Then you can use the GetDynamicMemberNames method:
@foreach(var item in data.GetDynamicMemberNames()){
<strong>@item:</strong> @data[item]<br />
}
Note that if you want to introduce spaces in your aliases, you MUST enclose the alias in [ ] brackets. Also, if you do this, you cannot use dynamic properties to reference the data. You have to use the indexer approach, so
freakysquash
Member
228 Points
155 Posts
Replace/Remove Columns returned from GetDynamicMemberNames
Feb 08, 2012 05:21 AM|LINK
In a tutorial posted by Mike, Clickable webGrid Row, once a row was clicked, it will be redirected into another page displaying the data related to the clicked row. At the foreach section, column names from the database were dynamically displayed. what if I want to remove some columns that I don't want to be displayed? How about renaming some of the columns? Thank you for responses!
@foreach(var item in data.GetDynamicMemberNames()){ <strong>@item:</strong> @data[item]<br /> }GmGregori
Contributor
5446 Points
735 Posts
Re: Replace/Remove Columns returned from GetDynamicMemberNames
Feb 08, 2012 06:34 AM|LINK
If you want to do this modification for all the detail pages, the easiest way is changing the SELECT statement. The details.cshtml would become, e.g.:
@{ Page.Title = "Details"; var db = Database.Open("Northwind"); // var query = "SELECT * FROM Customers WHERE CustomerID = @0"; var query = "SELECT CustomerID AS Id, CompanyName AS Company, ContactName AS Contact, " + "Address, City, PostalCode AS [Postal Code] FROM Customers WHERE CustomerID = @0"; var data = db.QuerySingle(query, UrlData[0]); } <h1>Details</h1> @foreach(var item in data.GetDynamicMemberNames()){ <strong>@item:</strong> @data[item]<br /> }Mikesdotnett...
All-Star
154852 Points
19855 Posts
Moderator
MVP
Re: Replace/Remove Columns returned from GetDynamicMemberNames
Feb 08, 2012 06:44 AM|LINK
You have (at least) two choices. First, don't use the code above. Create a template:
Second, use aliases in your SQL:
@{ var sql = @"SELECT CompanyName AS [Company Name], ContactName AS [Contact Name] FROM Customers WHERE CustomerId = 'ALFKI'"; var data = Database.Open("Northwind").QuerySingle(sql); }Then you can use the GetDynamicMemberNames method:
@foreach(var item in data.GetDynamicMemberNames()){ <strong>@item:</strong> @data[item]<br /> }Note that if you want to introduce spaces in your aliases, you MUST enclose the alias in [ ] brackets. Also, if you do this, you cannot use dynamic properties to reference the data. You have to use the indexer approach, so
will not work, whereas the following will:
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
freakysquash
Member
228 Points
155 Posts
Re: Replace/Remove Columns returned from GetDynamicMemberNames
Feb 08, 2012 06:58 AM|LINK
Thanks Mike! works perfectly. btw, I use ' ' insted of [ ] as the database I am using is MySQL.