最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - MVC redirect from view to controller with parameters - Stack Overflow

programmeradmin1浏览0评论

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.)

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.)

Share Improve this question edited Aug 14, 2014 at 8:08 benka 4,74235 gold badges49 silver badges58 bronze badges asked Aug 14, 2014 at 8:05 shalini gargshalini garg 311 silver badge3 bronze badges 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 }) } – user3559349 Commented Aug 14, 2014 at 8:13
Add a ment  | 

3 Answers 3

Reset to default 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');
}

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>

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>
发布评论

评论列表(0)

  1. 暂无评论