Note that there are some explanatory texts on larger screens.

plurals
  1. POCheckBoxFor in webgrid with model IEnumerable<T> and T.boolValue
    primarykey
    data
    text
    <p>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:</p> <pre><code>@model IEnumerable&lt;DFIMAX_MVC4.NCSJServices.AxdEntity_LedgerJournalTrans&gt; @{ ViewBag.Title = "ledgerJournalTrans"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt; Sagslinjer&lt;/h2&gt; &lt;p&gt; @Html.ActionLink("Create New", "Create") &lt;/p&gt; &lt;div&gt; @{ var grid = new WebGrid(source: Model, defaultSort: "Name"); var cols = grid.Columns( grid.Column(header: "", style: "ColPost", format: (item) =&gt; item.GetSelectLink("Bogfør")), grid.Column(header: "", style: "ColDelete", format: (item) =&gt; @Html.ActionLink("Slet", "Delete", new { id = item.RecId })), grid.Column(header: "", style: "ColEdit", format: (item) =&gt; @Html.ActionLink("Ret", "Edit", new { id = item.RecId })), grid.Column("NCPosted", style: "ColPosted", header: "Bogført", format: (item) =&gt; @Html.CheckBoxFor(item.NCPosted)), grid.Column("TransDate", style: "ColDate", header: "Dato", format: (item) =&gt; 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) =&gt; string.Format("{0:C}", item.AmountCurDebit)), //grid.Column("AmountCurCredit", header: "Kredit", format: (item) =&gt; string.Format("{0:C}", item.AmountCurCredit)) grid.Column(columnName: "Amount", header: "Beløb", style: "ColAmount", format: (item) =&gt; string.Format("{0:C}", (item.AmountCurDebit != null ? item.AmountCurDebit : 0) - (item.AmountCurCredit != null ? item.AmountCurCredit : 0))) ); } @grid.Table( columns: cols, footer: @&lt;table class="footer"&gt; &lt;tr&gt; &lt;td class="ColPost"&gt; Total &lt;/td&gt; &lt;td class="ColDelete"&gt; &lt;/td&gt; &lt;td class="ColEdit"&gt; &lt;/td&gt; &lt;td class="ColPosted"&gt; &lt;/td&gt; &lt;td class="ColApproved"&gt; &lt;/td&gt; &lt;td class="ColDate"&gt; &lt;/td&gt; &lt;td class="ColCase"&gt; &lt;/td&gt; &lt;td class="ColInitials"&gt; &lt;/td&gt; &lt;td class="ColAccountType"&gt; &lt;/td&gt; &lt;td class="ColAccount"&gt; &lt;/td&gt; &lt;td class="ColTxt"&gt; &lt;/td&gt; &lt;td class="ColVoucher"&gt; &lt;/td&gt; &lt;td class="ColAmount"&gt; Totalbeløb &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt;, tableStyle: "TransListTable", headerStyle: "TranslistHeader", footerStyle: "TransListFooter", alternatingRowStyle: "TransListAlternatingRow", selectedRowStyle: "TransListSelectedRow", rowStyle: "TransListRow") &lt;/div&gt; </code></pre> <p>The problem is this line:</p> <pre><code>grid.Column("NCPosted", style: "ColPosted", header: "Bogført", format: (item) =&gt; @Html.CheckBoxFor(item.NCPosted)), </code></pre> <p>The error produced is this:</p> <blockquote> <p>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.</p> </blockquote> <p>Why is it, that on my other lines, the format: (item) => works, but not on this checkbox ? </p> <p>I have also tried:</p> <pre><code>grid.Column("NCPosted", style: "ColPosted", header: "Bogført", format: (item) =&gt; @Html.CheckBoxFor((bool)item.NCPostedValue)), </code></pre> <p>Where the error produced is this :</p> <blockquote> <p>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</p> </blockquote> <p>Any help would be much appreciated.</p> <p>Kind regards, </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload