ASP.MVC 2列表项上的模态表单

时间:2011-09-01 17:14:51

标签: jquery asp.net-mvc

我有以下请求列表:

<% foreach (var item in Model) { %>
    <tr>
        <td><%: item.Name %></td>
        <td>
            <%: Html.ActionLink("Accept", "Accept", new { id = item.Id } %>
            <%: Html.ActionLink("Reject", "Reject", new { id = item.Id } %>
        </td>
   </tr>
<% } %>

当有人拒绝请求时,我想呈现一个jQuery模式表单,以便他们可以进入动机。所以我用以下代码替换了拒绝行:

<label id="<%: item.Id %>" class="falselink" title="<%: "Reject" %>">Reject</label>

并将表单的div添加到html的末尾。

现在是时候事情变得混乱了...... IMO包含模态形式的div应该包含一个PartialView。类似的东西:

<div id="modal-form" title="Reject Request">
    <% Html.RenderPartial("RejectMotive", itemId);
</div>

我需要在jquery部分获取并传递Id:

$('.falselink').click(function () {
    // get the item id and pass it to the modal-form
    $('#modal-form').dialog('open');
});

这是处理这种情况的正确方法吗?我如何传递给jquery然后传递给Partial View?

1 个答案:

答案 0 :(得分:1)

您可以在此表单中放置一个隐藏字段,该字段将包含项ID,并在打开对话框之前指定其值:

$('.falselink').click(function () {
    // you used the id of the label to store the item id
    // It would be better to use a HTML5 data-* attribute on the
    // label for this purpose such as data-id for example as ids 
    // cannot start with numbers. In this case you would simply
    // fetch it like this: var itemId = $(this).data('id');
    var itemId = this.id; 

    // set the value of the hidden field inside the form to contain the 
    // item id
    $('#hiddenItemId').val(itemId);

    // show the modal
    $('#modal-form').dialog('open');
});