Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net MVC making cell contents as link in Grid.MVC

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.

like image 469
RandomUser Avatar asked Jun 05 '14 12:06

RandomUser


People also ask

How will you create hyperlink in MVC?

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.

What is HTML ActionLink in MVC?

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.


1 Answers

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());
like image 88
RandomUser Avatar answered Sep 18 '22 15:09

RandomUser