2

嗨,我对 MVC 很陌生,我目前正在尝试使用 MVC 3 和 Entity Framework 4.1 实现一个小型人力资源系统。现在,我被困在一个看起来很简单的任务上,即显示一个表格,其中列出了最近请病假的员工,并且在每个表格行上,我都有一个复选框,用户将检查该员工是否提供了病假床单。我还希望在每一行上都有一个按钮/链接/等,单击它时会调用对控制器方法的调用,并以某种方式将复选框的选中值与病假应用程序的 id 一起传递。我尝试使用操作链接,但我有点难过我将输入的路由值意味着复选框的选中值。同样的功能只需要我几分钟就可以在网络表单中完成,但就像我说的那样 我是 MVC 的新手,因为这似乎是一个非常简单的任务,所以可能在某个地方有一个非常简单的解决方案,但我就是无法为我的生活弄明白。如果我猜这可能需要一点 jquery(顺便说一句,有时对我来说仍然像一门外语)和隐藏字段?啊,我不知道。有人可以帮我吗?提前致谢。

这是我的观点:

    @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>
4

1 回答 1

0

这是我头顶上的一个快速解决方案(由于我的懒惰而缩短了表格):

HTML/剃须刀

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

查询脚本

<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在 url 的末尾放置一个,然后再继续。

编辑:

路线

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");
    }
于 2012-07-11T01:23:22.203 回答