2012-07-10 36 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