I am developing an ASP.net MVC application.
Earlier I used the following code to display a table of products.
foreach (var item in Model)
{
<div class="row">
<div class="cell">
@Html.ActionLink(item.productName, "ViewProduct", "Showcase",
new { productID = item.productID }, null)
</div>
<div class="cell">
@item.quantity
</div>
<div class="cell">
@item.price
</div>
</div>
}
It worked fine, I was able to make the Nx1'st element as a link to redirect to the view that displays the product's details.
Then I wanted to implement GridView to display products table using MVC.Grid Nuget package.
Grid works fine but I can't make the Nx1'st element as link so that I can redirect.
I tried out this but it's not working.
@Html.Grid(Model).Columns(columns =>{
columns.Add(model => model.productName).Titled("Name").Filterable(true)
.RenderValueAs(o => Html.ActionLink(o.productName, "ViewProduct", "Showcase",
new { productID = o.productID }, null));
columns.Add(model => model.quantity).Titled("Quantity Available");
columns.Add(model => model.price).Titled("Price");
}).WithPaging(3).Sortable(true)
The output that I get in the Nx1 cell is:
<a href="/Showcase/ViewProduct/?productID=15">Galaxy Note 3</a>
Desired output: Galaxy Note 3
Please help me out. Thanks.
To create a hyperlink based on controller action method, we can use Html. ActionLink helper methods. We can also use different overload methods of the Html. ActionLink to format our hyperlink or apply css styles or pass object parameters as we have passed in the Url.
Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.
This solved the problem
columns.Add(model => model.productName).Titled("Name")
.Filterable(true).Sanitized(false).Encoded(false).
RenderValueAs(model => Html.ActionLink(model.productName,
"ViewProduct", "Showcase", new { productID = model.productID }, null)
.ToHtmlString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With