0

I tried several answers posted on stackoverflow. However, the following doesn't seem to work:

@Html.TextArea("Comments", Model.Comments, Model.ReadOnly ? new { @disabled = "disabled"} : null)

I also tried:

@Html.TextArea("Comments", Model.Comments, Model.ReadOnly ? new { disabled = "disabled"} : null)

Any idea what am I doing wrong?

4
  • Did you read my question? I already tried that Commented Feb 24, 2016 at 21:18
  • You did not specify what answers you already looked at, there's no way I could know whether you saw that before or not. Commented Feb 24, 2016 at 21:19
  • Sorry, thats my mistake. Thanks for pointing out Commented Feb 24, 2016 at 21:36
  • Voting to close due to the desired behavior being unclear. Commented Feb 24, 2016 at 21:39

3 Answers 3

0

I would use a custom variable to set it. I don't think the conditional is acceptable in the arguments of the HtmlHelper.

@{
    var htmlAttributes = Model.ReadOnly ? new { disabled = "disabled" } : null;
}

@Html.TextArea("Comments", Model.Comments, htmlAttributes)
Sign up to request clarification or add additional context in comments.

2 Comments

I am going to try that! Any idea if I can add similar parameters to BOTH the true and false conditions? Without mentioning that in both
As long as the result is a valid collection of attributes you should be able to construct it that way.
0

I don't think that MVC likes a null here. What you need as a third parameter (which expects object) is a default empty anonymous instance instead of null:

Model.ReadOnly ? (object)new { disabled = "disabled" } : (object)new { }

Comments

0

Try wrapping your ternary operation in parentheses

@Html.TextArea("Comments", Model.Comments, (Model.ReadOnly ? new { @disabled = "disabled"} : null) )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.