2
foreach (var m in Model.Roles.ToList())
{
    <div>
        @Html.Label(m.Id, m.Name)
        @Html.CheckBox(m.Id, m.Selected, new {id = @m.Id })
    </div>
}

Where Model.Roles.ToList contains

Id: "5ef15ae7-7e34-4736-98cd-8c472f41869f", Name: "Administrators", Selected: false 
Id: "c11fa932-282a-4273-9cc4-ab181aca5d7a"  Name: "Users",      Selected: true 
Id: "c16f8cad-bc32-4539-b2e7-ad82726946b6"  Name: "Developers",     Selected: false

and the following output is generated:

    <div>
        <label for="">Administrators</label>
        <input id="5ef15ae7-7e34-4736-98cd-8c472f41869f" name="5ef15ae7-7e34-4736-98cd-8c472f41869f" type="checkbox" value="true" /><input name="5ef15ae7-7e34-4736-98cd-8c472f41869f" type="hidden" value="false" />
    </div>
    <div>
        <label for="c11fa932-282a-4273-9cc4-ab181aca5d7a">Users</label>
        <input checked="checked" id="c11fa932-282a-4273-9cc4-ab181aca5d7a" name="c11fa932-282a-4273-9cc4-ab181aca5d7a" type="checkbox" value="true" /><input name="c11fa932-282a-4273-9cc4-ab181aca5d7a" type="hidden" value="false" />
    </div>
    <div>
        <label for="c16f8cad-bc32-4539-b2e7-ad82726946b6">Developers</label>
        <input id="c16f8cad-bc32-4539-b2e7-ad82726946b6" name="c16f8cad-bc32-4539-b2e7-ad82726946b6" type="checkbox" value="true" /><input name="c16f8cad-bc32-4539-b2e7-ad82726946b6" type="hidden" value="false" />
    </div>

You will notice the first label for="" has no value, but when I run the debugger there is a value for this property. Any ideas as to what is happening?

UPDATE: The actual problem appears to be the first id starting with "5". If I replace that with a character, everything works as expected. Odd bug. I wish I knew where to report this.

WhiskerBiscuit
  • 4,795
  • 8
  • 62
  • 100
  • I poked into the method in ILSpy. It's using the first parameter to get model metadata from viewdata. This is confusing. –  Jan 30 '14 at 18:49

1 Answers1

0

Perhaps razor thinks it's generating a page that is HTML4 or older? In HTML4, an id could not start with a number; it had to be an alphabetic character. See this question for a discussion of valid HTML id values. After that, I guess we're looking at how Razor handles those rules.

Community
  • 1
  • 1
Corin
  • 2,317
  • 2
  • 32
  • 47