2014-01-26 14 views
1

我有一個頁面,顯示一個僱員名稱ID等表。有編輯鏈接與每個項目。當用戶點擊編輯鏈接時,它會打開一個包含員工詳細信息的新模式框。 模式窗口與父窗口具有相同的模型。事實上,我將相同的模型傳遞給模態窗口。MVC如何傳遞和讀取動態生成的行的ID值

請注意,當用戶點擊編輯鏈接時,我不想調用任何操作方法。當單擊編輯按鈕時,我只顯示詳細信息(在文本字段中)。當用戶在編輯後點擊保存按鈕,然後我調用動作來更新數據庫。

我使用的代碼第一種方法

請考慮下面的代碼

@model IEnumerable<WebApplication1.Models.employee> 

......

  @foreach (var item in Model) { 
       <tr> 
        <td> 
         @Html.DisplayFor(modelItem => item.name) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => item.age) 
        </td> 
        <td> 
         @Html.DisplayFor(modelItem => item.address) 
        </td> 
        <td> 
         @* I want to pass the value of employee ID when user click this link *@ 
         <a data-toggle="modal" data-target="#myModal" id="editlink1" >Edit..</a> 
        </td> 
       </tr> 
      } 

//////

   <!-- Modal --> 
       <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
        <div class="modal-dialog"> 
         <div class="modal-content"> 
          <div class="modal-header"> 
           <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
           <h4 class="modal-title" id="myModalLabel">Modal title</h4> 
          </div> 
          <div class="modal-body"> 
           Name of the Employee clicked is <TODO> 
          </div> 
          <div class="modal-footer"> 
           <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
           <button type="button" class="btn btn-primary">Save changes</button> 
          </div> 
         </div><!-- /.modal-content --> 
        </div><!-- /.modal-dialog --> 
       </div><!-- /.modal --> 

我想知道如何在編輯鏈接單擊時傳遞員工ID,以及如何訪問Modal窗口中傳遞的值。

請注意模型索引&模態窗口是一樣的,這是因爲在模態窗口中我有選項移動到下一個記錄。

+0

我們在哪裏你'jQuery'代碼? – Satpal

+0

對不起,我是Jquery和MVC的新手。我還沒有爲此編寫任何jQuery代碼,實際上我想知道如何傳遞可以在Modal窗口中訪問的ID。我們可以用Jquery做這個嗎? –

回答

0

你應該通過Javascript或最好使用jQuery來做到這一點。每次當你不需要打電話給服務器時,使用一些客戶端編程(JS)是一個優選的解決方案。

要快速啓動jQuery的:

http://learn.jquery.com/

1

在生成動態通過foreach循環的鏈接,給的鏈接像linkEmployee一類,並給出一個ID來個個像lnkEmployee_ + EmployeeID 因此,您的鏈接將像lnkEmployee_2,lnkEmployee_4,lnkEmployee_5,其中2,4和5將是您的員工ID。

$(document).ready(function(){ 
    var EmployeeID=''; 

    $('.linkEmployee ').click(function(){ 
    //Get the ID of currently clicked link like. 
    var ID= $(this).attr('id'); 

    //split the ID by '_' and get the employee ID as below 
    EmployeeID=ID.split('_')[1]; 

    //Employee ID is the desired ID on the Edit click which will be present globally       everyehere in the javascript. you can use this ID to open your MODAL popup . 


    lblHiddenEmployeeID.text(EmployeeID); 
    })   
}) 

得到這個全球僱員ID後,您可以指定這個ID作爲文本標籤,它存在於你的模式對話框作爲隱藏字段。

您可以在您的MODAL BOX中以您希望的方式使用此標籤文本。

希望這可以幫助你。

0

使用部分或普通視圖

foreach (var item in Model) 
    { 
    ... 
    ... 
    <p class="row-doctor-details-row1"><a href="#" data-toggle="modal" data-target="#@item.Id"> @item.Name</a></p> 
    .... 
    ... 
    <div class="modal fade" id="@item.Id" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 
    ... 
    your details to show 
    ... 
    <div> 
    } 
相關問題