genxgeek
genxgeek

Reputation: 13367

How to inject an @Html.Action link in an existing table using jquery?

I have the following table in my view.

<table>
<tr>
   <th>Name</th>
   <th>Start</th>
   <th>End</th>
</tr>
<tr>
   <td>Whatever</td>
   <td>3/14/2012</td>
   <td>3/31/2012</td>
</tr>
<tr>
   <td>Whatever2</td>
   <td>3/15/2012</td>
   <td>3/30/2012</td>
</tr>
</table

What I want to be able to do in in my view is turn each col row for "Name" (Whatever, Whatever2) into a corresponding actionlink based on the corresponding id that is passed into the view (that will later provide drill down into another view). How can I do this using jquery?

Something like:

<script type="text/javascript">
    @foreach(var row in Model)
    {
        <text>$('#rowcolid.text(@(Html.ActionLink(<name of the row cell for "Name" col>, "Details", new {id = modelItem => item.id})</text>
    }
</script>

Upvotes: 0

Views: 1567

Answers (2)

genxgeek
genxgeek

Reputation: 13367

This is what I used for a solution in my view (it's a hack but it achieved what I needed considering the constraints):

<script type="text/javascript">
@{
   int ii = 0;
   foreach(var row in Model.Items)
   {   
       <text>$('td:first-child:eq(@(ii))').html('@Html.ActionLink(row.EntityName, "Details", new {id = row.EntityId })')</text>
       ii++;
   }
}
</script>

Upvotes: 1

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93484

You can't "inject" an ActionLink in jQuery. jQuery is a client side technology, an ActionLink is a server side technology. What jQuery sees is only what the browser sees, and the browser does not see anything called an ActionLink. It only sees the results of an ActionLink, which is.. a standard, ordinary, every day link.

Upvotes: 1

Related Questions