使用IEnumerable在List视图中传递Model.Id

时间:2014-08-28 04:53:45

标签: asp.net-mvc twitter-bootstrap

我希望在删除行记录之前显示模态cofirm。但我无法通过" Model.Id "在我看来。这是我的错误:"无法将方法组分配给匿名类型属性"。谁能为我的问题提供解决方案?

   @model IEnumerable<Blog.Web.Areas.AdminPanel.Models.Accounts.AccountViewModel>

   @foreach (var item in Model)
   {
        // get list user in here
    // delete button
        <a data-toggle="modal" href="#ConfirmModal" class="btn btn-danger"><i class="halflings-icon white trash"></i></a>
   }

   @using (Html.BeginForm("Delete", "Account", new { id = Model.Id }, FormMethod.Post))
   {
      // show bootstrap modal confirm delete
      @Html.Partial("../PartialView/_ConfirmDeleteModal", Model)
   }

1 个答案:

答案 0 :(得分:0)

我相信这就是你的意图:

@model IEnumerable<Blog.Web.Areas.AdminPanel.Models.Accounts.AccountViewModel>

@foreach (var item in Model)
{
    // get list user in here
    // delete button
    <a data-toggle="modal" href="#ConfirmModal" class="btn btn-danger"><i class="halflings-icon white trash"></i></a>
    @using (Html.BeginForm("Delete", "Account", new { id = item.Id }, FormMethod.Post))
    {
        // show bootstrap modal confirm delete
        @Html.Partial("../PartialView/_ConfirmDeleteModal", item)
    }
}

另一种选择

如果你所有人都是以Bootstrap为主题的确认对话框,我建议改用Bootbox(http://bootboxjs.com/);然后你的观点看起来像这样:

@model IEnumerable<Blog.Web.Areas.AdminPanel.Models.Accounts.AccountViewModel>

@foreach (var item in Model)
{
    // get list user in here
    // delete button
    @using (Html.BeginForm("Delete", "Account", new { id = item.Id }, FormMethod.Post))
    {
        @Html.Hidden("Id", item.Id)
        <button data-id="@item.Id" type="submit" class="btn btn-danger delete-item"><i class="halflings-icon white trash"></i></button>
    }
}

@section scripts
{
    <script src="~/scripts/bootbox.min.js"></script>
    <script>
        $(function(){
            $(document).on('click', '.delete-item', function(e){
                e.preventDefault();

                var form = $(this).closest('form');
                bootbox.confirm("Are you sure?", function(result) {
                    if(result === true){
                        form.trigger('submit');
                    }
                }); 
            });
        });
    </script>
}