5

I'm trying to define a Boolean.cshtml to use with EditorFor using the following code:

@{
   var o = ViewData.ModelMetadata;
}

<div class="editor-for">
    @Html.CheckBox(o.PropertyName,
                   ViewData.Model,
                   new { @class="tickbox-single-line" })
</div>

Unfortunately, Model is null and I get the following non-descriptive error:

CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'CheckBox' 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.

After reading this I tried the following:

@Html.CheckBox(o.PropertyName,
               ViewData.Model ?? false,
               new { @class="tickbox-single-line" })

But I get exactly the same error. The View code is simply:

@Html.EditorFor(m => m.RememberMe)

What am I doing wrong in Boolean.cshtml?

2 Answers 2

8

Not sure this is the best way, but I think you just have a problem with ViewData.Model, which is (if I'm not wrong), the dynamic problematic thing.

So... cast it !

@{
   var o = ViewData.ModelMetadata;
   bool model = (bool)ViewData.Model;
}

<div class="editor-for">
    @Html.CheckBox(o.PropertyName,
                   model,
                   new { @class="tickbox-single-line" })
</div>

but if I'm not wrong, an EditorTemplate would rather look like

@model bool

<div class="editor-for">
   @Html.CheckBox("", Model, new{@class="tickbox-single-line"})
<div>
Sign up to request clarification or add additional context in comments.

5 Comments

I'm confused by your second snippet. What does @model bool do?
@LuisFerrao well, it's just the usual presentation of an Editor Template for a bool
That doesn't really tell me what it does, but I guess at this point I should just RTFM. In any case, this works, so I'll mark it as answer.
@LuisFerrao it's just like any model in any view. In that case, the model is... bool
I see! It's strange though, I tried the same syntax for a string editor template: "@model string" and then feed "Model" as a value just like you did for the checkbox, and whatever text I enter in the textbox is never fed back to the model.
4

this is a bit on the late side, but you can also use CheckBoxFor to simplify it.

My EditorTemplate is simply:

@model bool

@Html.CheckBoxFor(m => m)

2 Comments

I tried this - it didn't work. The error is: The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.Boolean'.
if the model item passed is a null then that isn't a bool - it's a Nullable bool. Instead, use @model bool? @Html.CheckBoxFor(m => m)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.