2012-07-10 61 views
2

嗨,我對MVC很陌生,目前我正嘗試使用MVC 3和Entity Framework 4.1實現一個小型人力資源系統。現在我被困在看起來像一個非常簡單的任務,顯示一張表,列出了最近因工作而放棄病假的僱員,並且在每個表格行中,我有一個複選框,用戶將檢查該僱員是否提供了病假片。我也想在每一行上有一個按鈕/鏈接/等,當單擊它時會調用對控制器方法的調用,並以某種方式將複選框的選中值與病假應用程序的ID一起傳遞。我嘗試使用動作鏈接,但是我對輸入的內容感到困惑,因爲routevalue意味着複選框的選中值。同樣的功能只需要幾分鐘時間就可以完成網頁表單,但就像我說的我是MVC的新手一樣,因爲這看起來像一個非常簡單的任務,可能有一個非常簡單的解決方案,但我無法想象它爲了我的生活。如果有什麼我猜這可能需要一點jQuery(這btw仍然看起來像我的外語有時)和隱藏的領域? Arrgghhh我不知道。有人可以幫助我嗎?提前致謝。MVC 3根據是否選中複選框來處理表格行

這是我的觀點:

@model IEnumerable<LeaveSoft.Models.PendingSickLeave> 

    <table> 
    <tr> 


    <th> 
     Applicant's Name 
    </th> 

    <th> 
     Leave Start 
    </th> 
    <th> 
     Leave End 
    </th> 
    <th> 
     Number Of Days 
    </th>  

    <th> 
     Reason 
    </th> 

    <th> 
     Confirm 
    </th> 
    <th> 

    </th> 
</tr> 

    @foreach (var item in Model) { 
<tr> 


    <td> 
     @Html.DisplayFor(modelItem => item.UserFullName) 
    </td> 

    <td> 
     @Html.DisplayFor(modelItem => item.StartDate) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.EndDate) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.NumOfDays) 
    </td> 


    <td> 
     @Html.DisplayFor(modelItem => item.LeaveReason) 
    </td> 

    <td> 
     @Html.CheckBox("chkConfirm") 


      @Html.Label("", "Sick Sheet Provided") 

    </td> 

    <td> 
     @Html.ActionLink("Process", "ProcessSickLeave", new { id = item.ApplicationID, sicksheet = "THIS IS THE PART I DON'T KNOW" }) 

    </td>  
</tr> 
    } 

</table> 

回答

0

這裏有一個快速的解決了我的頭頂部(表縮短由於我的懶惰):

HTML/RAZOR

<table> 
    <tr> 
     <th>ApplicantName</th> 
     <th>Confirm</th> 
     <th></th> 
    </tr> 
    @foreach (var item in Model.PendingSickLeave) // this is a List<> 
    { 
    <tr> 
     <td>@item.UserFullName</td> 
     <td>@Html.CheckBoxFor(m => item.SickSheet, new { @id = "chk" + @item.ApplicationID })</td> 
     <td>@Html.ActionLink("Process", "ProcessSickLeave", new { id = item.ApplicationID }, new { @class="submit", @data_val = item.ApplicationID })</td>  
    </tr> 
    } 
</table> 

jQuery腳本

<script> 
    $(function() { 
      // captures click event. appends the sicksheet querystring on the url. 
     $('a.submit').click(function (e) { 
      e.preventDefault(); 
      var id = $(this).attr('data-val'); 
      var chk = $('#chk' + id); 
      if ($(chk).is(':checked')) 
       $(this).attr('href', $(this).attr('href') + '?sicksheet=true'); 
      else 
       $(this).attr('href', $(this).attr('href') + '?sicksheet=false'); 
     }); 

    }); 
</script> 

基本上,當你點擊Process actionlink時,jquery會查找一個匹配ID的複選框,如果選中了它,它會在網址的末尾放置一個?sicksheet=true,然後繼續。

編輯:

ROUTE

routes.MapRoute("ProcessSickLeave", "process/processsickleave/{applicationid}", 
        new { controller = "Process", action = "ProcessSickLeave" }); 

控制器

public ActionResult ProcessSickLeave(ProcessViewModel m) 
    { 
     return Content("ID = " + m.ApplicationID + 
        "<br/> Has Sick sheet: " + m.SickSheet.ToString(), "text/html"); 
    } 
+0

感謝您的及時回覆。我嘗試了你的建議,但當我點擊過程鏈接時,我得到以下錯誤:「參數字典包含空參數'SickSheet'的非空類型'System.Boolean'....」 – 2012-07-11 02:37:58

+0

i'我會更新我的答案,並附上一些額外信息 – jzm 2012-07-11 05:52:56

+0

Woohooo。是的,現在工作。十分感謝你的幫助。非常感激.. – 2012-07-11 23:22:50