0

I've been strugling with getting a small thing to work. I want to display a checkbox for a specific field in my model. My view has a IEnumerable as the @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>

The problem is this line:

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

The error produced is this:

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper>' has no applicable method named 'CheckBoxFor' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

Why is it, that on my other lines, the format: (item) => works, but not on this checkbox ?

I have also tried:

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

Where the error produced is this :

Compiler Error Message: CS1928: 'System.Web.Mvc.HtmlHelper>' does not contain a definition for 'CheckBoxFor' and the best extension method overload 'System.Web.Mvc.Html.InputExtensions.CheckBoxFor(System.Web.Mvc.HtmlHelper, System.Linq.Expressions.Expression>)' has some invalid arguments

Any help would be much appreciated.

Kind regards,

tereško
  • 58,060
  • 25
  • 98
  • 150

2 Answers2

0

Ok, try this:

format: (item) => InputExtensions.CheckBoxFor(Html, item.NCPosted)
YD1m
  • 5,845
  • 2
  • 19
  • 23
  • It then says: Compiler Error Message: CS0136: A local variable named 'item' cannot be declared in this scope because it would give a different meaning to 'item', which is already used in a 'parent or current' scope to denote something else – Asser Fahrenholz Jun 25 '13 at 09:12
  • Try `InputExtensions.CheckBoxFor()` – YD1m Jun 25 '13 at 09:30
  • Its now: Compiler Error Message: CS1501: No overload for method 'CheckBoxFor' takes 1 arguments. – Asser Fahrenholz Jun 25 '13 at 09:35
  • Of course, you need to call it this way `InputExtensions.CheckBoxFor(Html, item.NCPosted)` – YD1m Jun 25 '13 at 09:43
  • Error is now: Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'System.Web.Mvc.Html.InputExtensions.CheckBoxFor>(System.Web.Mvc.HtmlHelper>, System.Linq.Expressions.Expression,bool>>)' has some invalid arguments – Asser Fahrenholz Jun 25 '13 at 10:07
  • Argument 2: cannot convert from 'bool' to 'System.Linq.Expressions.Expression,bool>>' – Asser Fahrenholz Jun 25 '13 at 10:11
0

I figured it out:

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 "";
            }
        }));