patterncsharpMinor
A set of Razor HTML helpers for disabling elements
Viewed 0 times
helperselementssetfordisablinghtmlrazor
Problem
I am developing a huge form with a lot of elements in it. The purpose of this form is editing one domain entity. Now, I finished developing the whole form and it just shows the bunch of text boxes, text areas, radio buttons and so on, allowing user to change values of entities. When user submits the form, it goes to server and server processes the data.
The form is based on Razor's HTML helpers:
the next request that I got is to make user can just view the data and cannot edit this. So I should make the elements on the form disabled based on some condition. I inject special property to view model indicating ability to edit data:
Ok, now I would crawl over each element in the view and set
```
@{
object elementAttributes;
if (Model.Editable)
{
elementAttributes = new {@class = "field field-normal", style = "text-align: right;width:475px", Id = "Address"};
}
else
{
elementAttributes = new {@class = "field field-normal", style = "text-align: right;width:475px", Id = "Address", disabled = "disabled"};
}
Html.TextBoxFor(x => Model.Entities[i].Address, elemen
The form is based on Razor's HTML helpers:
(... code omitted for shortness ...)
@Html.TextBoxFor(x => Model.Entities[i].Address, new { @class = "field field-normal", style = "text-align: right;width:475px", Id = "Address" })
@Html.TextBoxFor(x => Model.Entities[i].SupplyDate, "{0:dd.MM.yyyy}", new { @class = "field field-normal field-date has-datepicker", style = "text-align: right;", Id = "SupplyDate" })
@Html.TextBoxFor(x => Model.Entities[i].Quantity, new { @class = "field field-normal field-num", style = "text-align: right;", Id = "Quantity" })
(... code omitted for shortness ...)the next request that I got is to make user can just view the data and cannot edit this. So I should make the elements on the form disabled based on some condition. I inject special property to view model indicating ability to edit data:
public class MyEntitiesViewModel
{
(... code omitted for shortness ...)
public Entity[] Entities { get; set; }
public bool Editable { get; set; }
(... code omitted for shortness ...)
}Ok, now I would crawl over each element in the view and set
disabled attribute for needed elements like this:```
@{
object elementAttributes;
if (Model.Editable)
{
elementAttributes = new {@class = "field field-normal", style = "text-align: right;width:475px", Id = "Address"};
}
else
{
elementAttributes = new {@class = "field field-normal", style = "text-align: right;width:475px", Id = "Address", disabled = "disabled"};
}
Html.TextBoxFor(x => Model.Entities[i].Address, elemen
Solution
With the danger of not understanding your question entirely as it stands now, you have
Why do you reiterate over the attributes, and create loads of helpers which I don't understand why you need. I'm not entirely sure about the syntax just now, but wouldn't something like the following do the trick:
Variant over the same theme
If you don't want to use logic in the class you could also add a
And corresponding in the `` definition:
and then your view will be simpler and will contain no logic at all:
MyEntitiesViewModel with a enabled field. Why don't you use to trigger a CSS class for the entire entity which disables that entity?Why do you reiterate over the attributes, and create loads of helpers which I don't understand why you need. I'm not entirely sure about the syntax just now, but wouldn't something like the following do the trick:
@Html.TextBoxFor(x => Model.Entities[i].Address, new { @class = "field field-normal", style = "text-align: right;width:475px", Id = "Address" })
Variant over the same theme
If you don't want to use logic in the class you could also add a
EnabledClass property to your viewmodel, using something like the following:public string EnabledClass {
get { return Editable ? "enabled" : "disabled" }
}And corresponding in the `` definition:
and then your view will be simpler and will contain no logic at all:
Code Snippets
<tr data-id="@row.Id" class="entity-row @(@Model.Editable ? "enabled" : "disabled")"">
<td style="width: 475px">
<span class="num">
@Html.TextBoxFor(x => Model.Entities[i].Address, new { @class = "field field-normal", style = "text-align: right;width:475px", Id = "Address" })
</span>
</td>
</tr>public string EnabledClass {
get { return Editable ? "enabled" : "disabled" }
}<tr data-id="@row.Id" class="entity-row @Model.EnabledClass">Context
StackExchange Code Review Q#108497, answer score: 5
Revisions (0)
No revisions yet.