i have created a database and am displaying the data by using this code. However, i want the Purchase column to be a separate link to a new cshtml page for each item. I tried storing the page link in the database and that didnt work. How can i add a link
in the purchase column that will go to a new separate cshtml page
sorry i thougth the prevouis answer was correct. I have mulitple enters in my grid and would like each item to have a link under the purchase column that goes to there individual page. I tried to add an <a href></a> in the data base but that didnt work.
Doing this:
Yes - all the links point to the same page, but you have a querystring value that identifies which product link was selected by its part number. You get the details from the database for the selected product based on that value:
@{
var id = Request["id"];
var sql = "SELECT * FROM Products WHERE PartNumber = @0";
var product = Database.Open("MyDb").QuerySingle(sql, id);
}
<h2>Details</h2>
@product.Description
//etc
You do not want to have to create a separate file for every product. If you do that, there's no point in using ASP.NET.
thanks for the reply. I would like to create a separate file and use ASP.NET. It easier to up date the database than to edit the HTML code everytime there is a change. The reason for wanting a separate file is that it is my understanding that you will be
better indexed if you use the product description instead of an ID when naming the .cshtml page.
maxparkin
Member
7 Points
5 Posts
a new cshtml page from a webgrid
Feb 09, 2012 12:59 AM|LINK
i have created a database and am displaying the data by using this code. However, i want the Purchase column to be a separate link to a new cshtml page for each item. I tried storing the page link in the database and that didnt work. How can i add a link in the purchase column that will go to a new separate cshtml page
@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns (
grid.Column ("PartNumber" , format:@<text>@item.PartNumber</text>),
grid.Column ("Description", format:@<text>@item.Description</text>),
grid.Column ("Price", format:@<text>@item.Price</text>),
grid.Column("Purchase", format:@<text>@item.Purchase</text>)
)
)
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: a new cshtml page from a webgrid
Feb 09, 2012 06:54 AM|LINK
You can do it like this:
@grid.GetHtml( tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt", columns: grid.Columns ( grid.Column ("PartNumber" , format:@<text>@item.PartNumber</text>), grid.Column ("Description", format:@<text>@item.Description</text>), grid.Column ("Price", format:@<text>@item.Price</text>), grid.Column("Purchase", format:@<a href="newPage.cshtml?id=@item.PartNumber">@item.Purchase</a>) ) )Or you can enable Selections: http://www.mikesdotnetting.com/Article/168/The-WebGrid-Helper-Making-Selections
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
maxparkin
Member
7 Points
5 Posts
Re: a new cshtml page from a webgrid
Feb 09, 2012 11:23 AM|LINK
sorry i thougth the prevouis answer was correct. I have mulitple enters in my grid and would like each item to have a link under the purchase column that goes to there individual page. I tried to add an <a href></a> in the data base but that didnt work. Doing this:
grid.Column("Purchase", format:@<a href="newPage.cshtml?id=@item.PartNumber">@item.Purchase</a>
takes all the products to the same page. Can i have it so that is goes to each items individual .cshtml page?
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: a new cshtml page from a webgrid
Feb 11, 2012 07:06 AM|LINK
Yes - all the links point to the same page, but you have a querystring value that identifies which product link was selected by its part number. You get the details from the database for the selected product based on that value:
@{ var id = Request["id"]; var sql = "SELECT * FROM Products WHERE PartNumber = @0"; var product = Database.Open("MyDb").QuerySingle(sql, id); } <h2>Details</h2> @product.Description //etcYou do not want to have to create a separate file for every product. If you do that, there's no point in using ASP.NET.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
maxparkin
Member
7 Points
5 Posts
Re: a new cshtml page from a webgrid
Feb 11, 2012 02:26 PM|LINK
thanks for the reply. I would like to create a separate file and use ASP.NET. It easier to up date the database than to edit the HTML code everytime there is a change. The reason for wanting a separate file is that it is my understanding that you will be better indexed if you use the product description instead of an ID when naming the .cshtml page.
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: a new cshtml page from a webgrid
Feb 11, 2012 07:19 PM|LINK
You can do all sorts of things with your URLs to improve SEO without having to produce a page per product. See these two articles:
http://www.mikesdotnetting.com/Article/165/WebMatrix-URLs-UrlData-and-Routing-for-SEO
http://www.mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
maxparkin
Member
7 Points
5 Posts
Re: a new cshtml page from a webgrid
Feb 12, 2012 03:48 PM|LINK
thanks once again, you saved me a lot of time