具有模型IEnumerable <t>和T.boolValue </t>的webgrid中的CheckBoxFor

时间:2013-06-25 08:55:44

标签: asp.net-mvc compiler-errors webgrid checkboxfor

我一直在努力寻找一件小事。我想在模型中显示特定字段的复选框。我的视图有一个IEnumerable作为@model:

@model IEnumerable<DFIMAX_MVC4.NCSJServices.AxdEntity_LedgerJournalTrans>
@{
    ViewBag.Title = "ledgerJournalTrans";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Sagslinjer</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<div>
    @{
        var grid = new WebGrid(source: Model, defaultSort: "Name");
        var cols = grid.Columns(
               grid.Column(header: "", style: "ColPost", format: (item) => item.GetSelectLink("Bogfør")),
               grid.Column(header: "", style: "ColDelete", format: (item) => @Html.ActionLink("Slet", "Delete", new { id = item.RecId })),
               grid.Column(header: "", style: "ColEdit", format: (item) => @Html.ActionLink("Ret", "Edit", new { id = item.RecId })),
           grid.Column("NCPosted", style: "ColPosted", header: "Bogført", format: (item) => @Html.CheckBoxFor(item.NCPosted)),
               grid.Column("TransDate", style: "ColDate", header: "Dato", format: (item) => item.TransDate != null
                   ? item.TransDate.ToString("dd-mm-yyyy") : ""),
               grid.Column("NCCaseNumber", style: "ColCase", header: "Sag"),
               grid.Column("Approver", style: "ColInitials", header: "Init."),
               grid.Column("AccountType", style: "ColAccountType", header: "Type"),
               grid.Column("LedgerDimension.DisplayValue", header: "Part/Konto", style: "ColAccount"),
            //grid.Column("Art", header: "Art"),
               grid.Column("Txt", style: "ColTxt", header: "Reference"),
               grid.Column("Voucher", style: "ColVoucher", header: "Bilag"),
            //grid.Column("AmountCurDebit", header: "Debit", format: (item) => string.Format("{0:C}", item.AmountCurDebit)),
            //grid.Column("AmountCurCredit", header: "Kredit", format: (item) => string.Format("{0:C}", item.AmountCurCredit))
               grid.Column(columnName: "Amount", header: "Beløb", style: "ColAmount",
               format: (item) => string.Format("{0:C}", (item.AmountCurDebit != null ? item.AmountCurDebit : 0) - (item.AmountCurCredit != null ? item.AmountCurCredit : 0)))
           );
    }
    @grid.Table(
            columns: cols,
            footer: @<table class="footer">
                <tr>
                    <td class="ColPost">
                        Total
                    </td>
                    <td class="ColDelete">
                    </td>
                    <td class="ColEdit">
                    </td>
                    <td class="ColPosted">
                    </td>
                    <td class="ColApproved">
                    </td>
                    <td class="ColDate">
                    </td>
                    <td class="ColCase">
                    </td>
                    <td class="ColInitials">
                    </td>
                    <td class="ColAccountType">
                    </td>
                    <td class="ColAccount">
                    </td>
                    <td class="ColTxt">
                    </td>
                    <td class="ColVoucher">
                    </td>
                    <td class="ColAmount">
                        Totalbeløb
                    </td>
                </tr>
            </table>,
            tableStyle: "TransListTable",
            headerStyle: "TranslistHeader",
            footerStyle: "TransListFooter",
            alternatingRowStyle: "TransListAlternatingRow",
            selectedRowStyle: "TransListSelectedRow",
            rowStyle: "TransListRow")
</div>

问题在于这一行:

grid.Column("NCPosted", style: "ColPosted", header: "Bogført", format: (item) => @Html.CheckBoxFor(item.NCPosted)),

产生的错误是:

  

编译器错误消息:CS1973:'System.Web.Mvc.HtmlHelper&gt;'没有名为'CheckBoxFor'的适用方法,但似乎有一个名称的扩展方法。无法动态分派扩展方法。考虑转换动态参数或调用扩展方法而不使用扩展方法语法。

为什么,在我的其他方面,格式:( item)=&gt;有效,但不是这个复选框?

我也尝试过:

grid.Column("NCPosted", style: "ColPosted", header: "Bogført", format: (item) => @Html.CheckBoxFor((bool)item.NCPostedValue)),

产生错误的地方是:

  

编译器错误消息:CS1928:'System.Web.Mvc.HtmlHelper&gt;'不包含'CheckBoxFor'的定义和最佳扩展方法重载'System.Web.Mvc.Html.InputExtensions.CheckBoxFor(System.Web.Mvc.HtmlHelper,System.Linq.Expressions.Expression&gt;)'有一些无效的参数

非常感谢任何帮助。

亲切的问候,

2 个答案:

答案 0 :(得分:0)

好的,试试这个:

format: (item) => InputExtensions.CheckBoxFor(Html, item.NCPosted)

答案 1 :(得分:0)

我明白了:

columns.Add(grid.Column(header: "", style: "ColPost", format: (item) =>
        {
            bool t = ((DFIMAX_MVC4.NCSJServices.AxdEntity_LedgerJournalTrans)item.Value).NCPosted.GetValueOrDefault().Equals(DFIMAX_MVC4.NCSJServices.AxdExtType_JournalPosted.Yes) ? true : false;

            if (((DFIMAX_MVC4.NCSJServices.AxdEntity_LedgerJournalTrans)item.Value).LedgerDimension != null)
            {
                return Html.CheckBoxFor(x => t);
            }
            else
            {
                return "";
            }
        }));