Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

@Html.CheckBoxFor() falls back to values in ViewDataDictionary #1483

Closed
dougbu opened this issue Oct 30, 2014 · 4 comments
Closed

@Html.CheckBoxFor() falls back to values in ViewDataDictionary #1483

dougbu opened this issue Oct 30, 2014 · 4 comments
Assignees
Milestone

Comments

@dougbu
Copy link
Member

dougbu commented Oct 30, 2014

When the expression result is null e.g. in a Create view, @Html.CheckBoxFor() finds value (if any) in the ViewDataDictionary. This is incorrect and inconsistent with other HTML helpers. Expression-based helpers should not use ViewDataDictionary and the rest do not.

@dougbu
Copy link
Member Author

dougbu commented Oct 30, 2014

E.g. the following in a create view

ViewData["Alive"] = true;
...
<td>
    @Html.LabelFor(m => m.Alive)
</td>
<td>
    @Html.CheckBoxFor(m => m.Alive)
    or
    @Html.RadioButtonFor(m => m.Alive, value: false) False
    @Html.RadioButtonFor(m => m.Alive, value: true) True
</td>

results in
capture

The checkbox should not be selected. The radio button by contrast correctly shows the expression result is neither true or false.

@dougbu
Copy link
Member Author

dougbu commented Oct 30, 2014

This bug also affects the <input type="checkbox"/> tag helper.

Workarounds include not adding entries to ViewDataDictionary or avoiding null containers in the model (!!).

Fix is to change one line to ensure useViewData passed to DefaultHtmlGenerator.GenerateInput() is always false in CheckBoxFor() scenarios. Could back-port this fix to MVC 5.2 (bug was inherited) as well.

@yishaigalatzer yishaigalatzer added this to the 6.0.0-beta2 milestone Nov 26, 2014
@danroth27 danroth27 modified the milestones: 6.0.0-rc1, 6.0.0-beta2 Dec 11, 2014
dougbu added a commit that referenced this issue Jan 16, 2015
- show `@Html.CheckBoxFor()` reading from the `ViewDataDictionary`
- generated checkbox and radio buttons will be consistent once bug is fixed
dougbu added a commit that referenced this issue Jan 16, 2015
- #1483
- update tests to match

nits:
- cover a couple more `CheckBoxFor` test cases
- capitalize "CheckBox" consistently
@yishaigalatzer
Copy link
Contributor

Remove the sample and :shipit:

dougbu added a commit that referenced this issue Jan 17, 2015
- #1483
- update tests to match

nits:
- cover a couple more `CheckBoxFor` test cases
- capitalize "CheckBox" consistently
@dougbu
Copy link
Member Author

dougbu commented Jan 17, 2015

Fixed via 12565da

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants