1

I need to redirect to controller action from view. My code in View looks like this.

<table id="tableid">
  <thead>
    <tr>
      <th id="Sno">S No</th>
      <th>Name</th>
      <th>Status</th>
      <th>Action</th>
      </tr>
  </thead>
  <tbody >
    @foreach (var sd in Model.Details)
    {

      <tr id="trid">
        <td>@sd .Id</td>
        <td>@sd .Name</td>
        <td>@sd .Status</td>
        <td>

          <a id="actionId"   onclick="Clickfn()"  >
          </a>
          <script>
            function Clickfn() {
              window.location.href = '@Url.Action("UpdateCamp", "CampDashboard", new {id =@sd .Id })'
            }
          </script>

          </td>
      </tr>
    }
  </tbody>
</table>

Code returns last Id of @sd .Id while clicking on tag..how to get that particular cell value (cell in which tag is present.)

1
  • 3
    Why do it with javascript? You can just use @foreach (var sd in Model.Details) { @Html.ActionLink(sd.Name, "UpdateCamp", "CampDashboard", new {ID = sd.ID }) } Commented Aug 14, 2014 at 8:13

3 Answers 3

3

You can attach the value to the a element using a data attribute which you can then use in the click handler:

@foreach (var sd in Model.Details)
{
    <tr id="trid">
        <td>@sd .Id</td>
        <td>@sd .Name</td>
        <td>@sd .Status</td>
        <td>
            <a id="actionId" data-id="@sd.Id" href="#"></a>
        </td>
    </tr>
}
$('#tableid a').click(function(e) { 
  e.preventDefault();
  window.location.href = '@Url.Action("UpdateCamp", "CampDashboard")?id=' + $(this).data('id');
}
Sign up to request clarification or add additional context in comments.

Comments

2

Correct anchor tag as :-

<a id="actionId" onclick="Clickfn(@sd.Id)" >

and change Clickfn as :

 <script>
    function Clickfn(Id) {
     window.location.href ="/CampDashboard/UpdateCamp?id" + Id;
    }
 </script>

4 Comments

This will not work as you are passing a static value to the URL in the Clickfn fucntion.
@DavidG...it's not a static value its inside foreach just look at the question carefully..
That's better, though I was hoping you'd take on the bet! No idea how we'd exchange the points though, maybe I should do a feature request on meta haha
@downvoter plz explain what is wrong with this answer??
1

You should use Html.ActionLink, it returns an anchor element (a element) that contains the virtual path of the specified action.

<td>
  @Html.ActionLink(sd.Name, "UpdateCamp", "CampDashboard", new {id = @sd.Id})
</td>

OR

You can't pass JavaScript variable to Url.Action. futher Url.Action function will render a string. You should replace string.

You should pass @sd.Id as paramter to Clickfn()

Use

@foreach (var sd in Model.Details)
 {
    <td>
    <tr>
    <a id="actionId" onclick="Clickfn(@sd.Id)"></a>
    </td>
    </tr>
 }
<script>
    function Clickfn(id) {
        var url = '@Url.Action("UpdateCamp", "CampDashboard", new {id = -1})'; //Generate URL string using razor
        window.location.href = url.replace('-1', id); //replace ID value
    }
</script>

4 Comments

Thanks ..var url = '@Url.Action("UpdateCamp", "CampDashboard", new {id = -1})'; //Generate URL string using razor window.location.href = url.replace('-1', id); is working for me
@shalinigarg, I would recommed you to use Html.ActionLink
if i have to pass name and status also with Id in href.then how i can pass it.
@shalinigarg, use @Html.ActionLink(sd.Name, "UpdateCamp", "CampDashboard", new {ID = sd.ID, Name =sd.Name, Status=sd.Status }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.