1

I have a large form in a Razor view and want to make certain form elements disabled depending on the state of the model object I'm passing in. So some logic has to be defined to determine whether to show this element, make it read-only or make it editable.

My current thinking is leading me to define some Razor @helper's with the logic there, although I'm not sure if that's the best way to do it. Kind of like ...

@helper determineElementStatus(string modelProperty)
{
  if (modelProperty == someState) {
    @Html.TextBoxFor....etc
  }
}

@determineElementStatus(model.someProperty)

Indeed I'm not sure if the view is the right place. It is presentation logic in the fact that it changes the appearance of the form, but is it best place elsewhere and how??. Help would be appreciated.

1 Answer 1

5

In the View you can control the logic of the change css. eg Depending the value of the Model values i create the displayMode and apply it to the Html.

  @{
        string isInherited = Model.IsInheritedValue ? "editor-field inherColor" : "editor-field";
        object displayMode = Model.IsDisabled ? new { @disabled = "disabled", @class = isInherited } : (object)new { @class = isInherited };
    }

So now whenever you show

@Html.TextBoxFor(x => x.Value, displayMode)

The displayMode will determine how to show it. hope this helps.

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.