3

Within an admin page I originally was generating several checkboxes within a view as per below:

Model:

public class Foo
{
    public const string Bar = "SomeString";
}

View:

@Html.CheckBox(Foo.Bar)
@Html.Label(Foo.Bar)

However, I wanted to change the display name of several of the checkboxes, so I created a view model (to later add a display name attribute):

public class FooViewModel
{
     public string Bar
     {
         get { return Foo.Bar; }
     }
}

And modified the view:

@Html.CheckBox(Model.Bar)
@Html.LabelFor(m => m.Bar)

However, the view is now generating an error when rendering the checkbox:

String was not recognized as a valid Boolean

Note, that if I change the property name within the view model to anything other than "Bar" the view is rendered correct. EG:

public class FooViewModel
{
     public string WTF
     {
         get { return Foo.Bar; }
     }
}

@Html.CheckBox(Model.WTF)
@Html.LabelFor(m => m.WTF)

Can anybody explain to me why this error is occurring if my viewmodel property is named "Bar"?

Edit: I have updated my question slightly seeing as how, i'm generating some confusion. The view is used as a search form and the checkboxes are simply used for selecting "search critera".

I'm generating the checkbox in this fashion so the name / id of the checkbox is related to corresponding business logic within the controller.

I'm aware that code will not compile if property name / field name within the same class are identical. That is not the issue, as i'm simply initializing a property from constant within a different namespace.

4
  • @ChrisF No, that parameter would be a string: msdn.microsoft.com/en-us/library/dd460245.aspx Commented Mar 4, 2012 at 21:40
  • 1
    @Shark - ah thanks - it's been a while since I did this stuff. Comment deleted. Commented Mar 4, 2012 at 21:41
  • Are you referencing the property as though it is a static one? How did that even compile? Commented Mar 4, 2012 at 21:46
  • @Shark yes, that is correct. Everything works / compiles correctly as long as the property name of the 'view model' is different than that of the backing constant string from the base model. Commented Mar 4, 2012 at 22:00

2 Answers 2

1

You cannot have a constant called Bar and a property called Bar:

public class Foo
{
    public const string Bar = "SomeString";

    public string Bar
    {
        get { return Foo.Bar; }
    }
}

This particular code snippet is invalid C# and won't even compile.

This being said the CheckBox/CheckBoxFor helpers in ASP.NET MVC work with boolean values. So I really don't understand why are you even attempting to bind it to a string property and the purpose of this Bar string constant.

The correct would be to have the following view model:

public class MyViewModel
{
    [Display(Name = "som estring")]
    public bool MyBoolValue { get; set; }
}

and in the strongly typed view:

@Html.LabelFor(x => x.MyBoolValue)
@Html.CheckBoxFor(x => x.MyBoolValue)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Darin, I have updated my question slightly to avoid the confusion I was causing. The property is simply being initialized from a constant value from a class in a different namespace. They are not in the same class as per your example.
0

Using a different checkbox constructor resolves the issue:

@Html.CheckBox(Model.Bar, false)
@Html.LabelFor(m => m.Bar)

1 Comment

i still don't get what your trying to do. CheckBox needs to bind to a bool, not a string. You should be doing @Html.CheckBoxFor(model => model.SomeBooleanProperty)

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.