MVC3 HTML.Checkbox in "Details" view

asp.net-mvc-3 entity-framework html-helper

Question

I find very little out anywhere about how to utilize HTML helpers to render a "Details" view. First case is I have some bit fields in SQL Server that are bools in my pocos. This all works fine for the Create and Edit views but if I want a check box to show in the details view with it's appropriate state set but readonly...I can't figure out how to do it. I've got this:

<div class="display-field">@Html.CheckBox("laptops", Model.laptops, new { @readonly = "true" }) </div>

While it renders correctly the check box isn't readonly.

Same thing with handling fields that are DropDowns on Create/Edit but what about best practices for the Details view? What's the best way to resolve the FK to the displayable name representing that value?

Thanks in advance

1
1
3/14/2011 2:49:33 PM

Popular Answer

The correct way to render a readonly checkbox is this:

@Html.CheckBoxFor(x => x.laptops, new { @readonly = "readonly" })

Notice the usage of the strongly typed CheckBoxFor helper instead of CheckBox as you already have a strongly typed view. And obviously for this to work the laptops property on your model must be of type boolean as that's the only possible type that would make sense for a checkbox.

3
3/14/2011 9:00:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow