2011-09-10 53 views
0

我正在使用MVC3 Jquery和AJAX生成類似List/Details視圖。更新詳細信息使用Ajax查看

現在我有一些像這樣的代碼:

$(document).ready(function() { 
     $('#example tbody tr').click(function() { 
      alert('row was clicked'); 
      $('#Container').load('../task/create/').fadeIn("slow"); 

     }); 
    }); 

我的HTML是如下

<table id="example" border = "2"> 
    <thead> 
    <tr style="border-style:solid" class="simplehighlight"> 
     <th> 
      Name 
     </th> 
     <th> 
      Description 
     </th> 
     <th> 
      tblStatu 
     </th> 
     <th> 
      DueDate 
     </th> 
     <th> 
      AssignedTo 
     </th> 
     <th> 
      CreatedOn 
     </th> 
     <th> 
      CreatedBy 
     </th> 
     <th> 
      ModifiedOn 
     </th> 
     <th> 
      ModifiedBy 
     </th> 
     <th></th> 
    </tr> 
    </thead> 


    <tbody> 
@foreach (var item in Model) 
{ 
    <tr style="border-style:solid"> 
     <td style="border-style:solid"> 
      @item.Name 

      @*@Html.DisplayFor(modelItem => item.Name)*@ 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.Description) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.tblStatu.Name) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.DueDate) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.AssignedTo) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.CreatedOn) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.CreatedBy) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ModifiedOn) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ModifiedBy) 
     </td> 
     <td> 
      @Html.ActionLink("Edit", "Edit", new { id = item.TaskId }) | 
      @Html.ActionLink("Details", "Details", new { id = item.TaskId }) | 
      @Html.ActionLink("Delete", "Delete", new { id = item.TaskId }) 
     </td> 
    </tr> 
} 
    </tbody> 

現在,我如何添加參數在我的網址任務/ ... ..根據選擇如果行的表?

回答

1

喜歡的東西(未測試) -

$(document).ready(function() { 
     $('#example tbody tr').click(function() { 
      alert('row was clicked'); 
      var rowid = $(this).attr("id"); 
      $('#Container').load('../task/create/',{rowid: rowid}).fadeIn("slow"); 

     }); 
    }); 

應使用負載功能的[數據]參數來傳遞一些數據返回到您呼叫的地址 - 數據將被調回。

編輯

下面是最終工作的代碼 -

$(document).ready(function() { 
    $('#example tbody tr').click(function() { 
     var rowid = $(this).find('td#Name').html(); 
     //alert(rowid); // rowid = 1; 
     $('#Container').load('../task/edit/' + $.trim(rowid)).fadeIn("slow"); 
    }); 
}); 
+0

我不明白這部分? var rowid = $(this).attr(「id」); $('#Container')。load('../ task/create /',{rowid:rowid})。fadeIn(「slow」); – TeaLeave

+0

據我所知,與HTMl交流的唯一途徑是在此時通過TD和TR,那麼attr(「id」)如何能夠從中獲取任何信息? – TeaLeave

+0

這應該是你可以做的一個例子。從行中獲取一些數據(在這個例子中是行ID,但你可以得到你需要的),然後將這些數據發回到'../task/create/'URL。 – ipr101