0

I am trying to disable a dropdownlist based on a condition, I'm using:

var disabledAttr = exists ? "disabled=true" : ""; //exists is a boolean of course
@Html.DropDownListFor(x => x.Input.Divisions[index].DivisionPlayers[participantIndex].Team[playerIndex].Id,
                            new SelectList(Model.ParticipantsList, "Value", "Text", selected),
                            new { @class = "participant form-control", disabledAttr })

but the select box renders the attribute as:

 disabledattr="disabled=true"

How can I get it to render as simply 'disabled=true'?

Alfredo A.
  • 1,697
  • 3
  • 30
  • 43
BMills
  • 881
  • 1
  • 10
  • 24
  • Can you try changing the var disabledAttr = "disabled" instead of "disabled=true"? – Faisal Rashid Nov 07 '18 at 17:27
  • 1
    Possible duplicates of [Conditionally disable Html.DropDownList](https://stackoverflow.com/questions/2089468/conditionally-disable-html-dropdownlist) and [Disable enable dropdownlistfor based on model property in mvc](https://stackoverflow.com/questions/38226034/disable-enable-dropdownlistfor-based-on-model-property-in-mvc). – Tetsuya Yamamoto Nov 08 '18 at 01:51

1 Answers1

1

Your problem is that you are appending an attribute to the anonymous object that is named disabledAttr and has the value disabled=true.

What you need to achieve is to have property called disabled, with the value of true (or "true", not sure which one works, I cannot test it right now.)

Quick and dirty solution:

@{
    string @class = "participant form-control";
    object attrs = exists ? (object)new {@class, disabled = true} : new {@class};
}
@Html.DropDownListFor(x => x.Input.Divisions[index].DivisionPlayers[participantIndex].Team[playerIndex].Id,
    new SelectList(Model.ParticipantsList, "Value", "Text", selected), attrs )
Marcell Toth
  • 3,433
  • 1
  • 21
  • 36
  • answer looks great but getting a compile error of '}' expected, but the brackets are all definitely matching. weird. I'm trying a variation, will report back. – BMills Nov 07 '18 at 17:56
  • @BMills By bad, I forgot that we are in a razor view, so the first two lines need to be put in a block of code, otherwise the name `@class` conflicts with the razor syntax – Marcell Toth Nov 07 '18 at 18:11
  • Also tried : 'code' var attrs = exists ? @"new {@class=""participant form-control"", disabled = true}" : @"new {@class=""participant form-control""}";'code' But it doesn't escape the quotes properly for some reason so doesn't work. (wish i knew how to post code in comments) – BMills Nov 07 '18 at 18:11
  • See my update. P. s. you put code in comments the same way you put them in posts inline: use backticks: `` – Marcell Toth Nov 07 '18 at 18:13
  • Couldn't get it working Marcel so I've just gone for a dirty 'if,else' statement instead. If you'd like to provide a sample of it working then I'd be happy to mark as the correct answer, as then I'd know it's something to do with my code, not yours. – BMills Nov 08 '18 at 08:53
  • Code looks good based on the duplicate answers, int he end I've used one of this extension methods which makes it nice and clean. :-) – BMills Nov 08 '18 at 09:34