1

I have a list of KeyValuePairs (string, bool). I am iterating over the list using a for loop, but when I try and access the key of the kvp at the current index, all I get is the string "Key".

The KeyValuePair is {"DD", false}

Sorry if this is super obvious, but I can't find anyone haveing the same issue.

{
@Html.LabelFor(model => model.Client.PaymentChannels[i].Key)
}

I would expect the Key to be the string "DD", and not the string "Key".

Edited: Sorry I pasted in the wrong piece of code.

Bigtingz92
  • 129
  • 1
  • 15
  • I believe the first argument to `CheckBoxFor` is meant to provide the *name* of the check box property, by looking down the expression tree for the final property name. It's not meant to provide the *value* of the checkbox. – Jon Skeet Apr 29 '19 at 14:06
  • Right you are, sorry I pasted in the wrong part. The checkbox is correctly showing the value part of the kvp – Bigtingz92 Apr 29 '19 at 14:08
  • 1
    Right, the point is the same though: `LabelFor` deliberately extracts the property name, which in this case is `Key`. If you just want a label using that value, you can do that without an expression tree at all: `` – Jon Skeet Apr 29 '19 at 14:11
  • Thanks. That gave me an error to do with model directory, but was otherwise spot on. – Bigtingz92 Apr 29 '19 at 14:20

0 Answers0