0

I'm working on modifying Controller logic for two an MVC/Razor generated forms. While debugging my code in order to confirm that my new code is doing what I expected it to do I noticed the following inconsistent behavior.

Form1 defined in Form1.cshtml with ActionResult1 contains a checkbox generated using

@Html.CheckBoxFor(model => model.AttributeName, new Dictionary<string, object>() { { "onclick", "this.checked=!this.checked;" } })

Form2 defined in Form2.cshtml with ActionResult2 contains a checkbox generated using

@Html.CheckBoxFor(model => model.AttributeName)

They both share the same Controller but separate ActionResult methods. The troubling behavior is observed when I submit each form:

When both checkboxes are checked:

  • When I use the debugging console to see the value for the checkbox in Form1 using formCollection.GetValue("AttributeName") I see that it contains the string "true,false" therefore returning true when I perform the following convert

    Model.AttributeName=(bool)formCollection.GetValue("AttributeName").ConvertTo(typeof(bool))
    
  • When I use the debugging console to see the value for the checkbox in Form2 using formCollection.GetValue("AttributeName") I see that it contains the string "false,true" therefore returning false when I perform the following convert

    Model.AttributeName= (bool)formCollection.GetValue("AttributeName").ConvertTo(typeof(bool))`
    

Assuming both checkboxes are NOT checked:
- Both forms Form1 and Form2 return the string "false"

This inconsistent behavior is giving me a hard time because I have both ActionResult1 and ActionResult2 use the same following logic after I cast and assign the boolean to Model.AttributeName:

if (Model.AttributeName){ ModelState.AddModelError("AttributeName", "Checkbox is required."); }

Please help.

1 Answer 1

1

The problem was that both forms were not exactly identically coded in terms of their HTML helpers:

Form1 was declared using strictly HTML helpers inside its enclosing container. Something like this:

...
<li  data-role="fieldcontain">
@Html.CheckBoxFor(model => model.AttributeName,...
@Html.LabelFor(model => model.AttributeName, "Checkbox Label String")
<br/>
@Html.ValidationMessageFor(model => model.AttributeName)
</li>
...

Form2 was declared using a mix of HTML AND helpers inside its enclosing container. Something like this:

...
<li  data-role="fieldcontain">
<label>
<span style="float: left;">Checkbox Label String.</span>
@Html.CheckBoxFor(model => model.AttributeName)
<br/>
@Html.ValidationMessageFor(model => model.AttributeName)
</label>
</li>
...

Apparently it makes a difference, and in fact mixes up the way Razor handles the form if I decide to hard-code the label rather than using the @Html.LabelFor(... helper. Once I replicated Form1's structure in Form2 they both started returning the same values ("false" for not checked, "true,false" when checked).

Therefore assigning the right boolean accordignly; True for

Model.AttributeName=(bool)formCollection.GetValue("AttributeName").ConvertTo(typeof(bool))

when checked. False when not.

Sign up to request clarification or add additional context in comments.

Comments

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.