2

For some reason an html helper is outputting this html which doesnt validate.

the validator tells me

There is no attribute "Length"

<%= Html.CheckBox("Medicamentos", Model.Medicamentos) %>

is outputting

<input type="checkbox" value="true" name="Medicamentos" id="Medicamentos" checked="checked" length="4">
Charles
  • 50,943
  • 13
  • 104
  • 142
nacho10f
  • 5,816
  • 6
  • 42
  • 73
  • Also, do you have data annotations on your model? – Nick Larsen Jan 29 '10 at 22:22
  • I doubt it is outputting that directly. As far as I recall, your HtmlHelper call would, by default, render a checkbox without the checked="checked" attribute, and I suspect the length="4" is coming from somewhere else too. You might want to double check your code. – Chris Jan 29 '10 at 22:23
  • Many of the validity problems in ASP.NET can be solved by using `clientTarget` (http://msdn.microsoft.com/en-us/library/system.web.ui.page.clienttarget.aspx) to turn off the ridiculous browser-sniffing code the web controls use by default. I don't know about *this* one, as `length` is a bizarre, meaningless attribute that has never existed on `` on any browser I've ever heard of, but it might be worth a go. – bobince Jan 29 '10 at 22:25
  • @bobince, @chris -- the problem is that it is matching a method signature that considers the model property to be the html attributes for the element. I suspect there's a boolean property on the (complex) model property that needs to be directly addressed instead -- either checked or value. See my answer. – tvanfosson Jan 29 '10 at 22:39

1 Answers1

1

I assume that it's matching the signature that takes a string and an object since I don't know what Model.Medicamentos is. In that case it takes the properties of the object and turns them into attributes on the element. I suspect that you simply want to use the Checked attribute on the Model property specified as the default value of the checkbox, i.e.,

<%= Html.CheckBox( "Medicamentos", Model.Medicamentos.Checked ) %>

In, which case, assuming that Checked is boolean it will match the correct method signature on the helper extension.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • Ah! Yes, the overload causing the wrong CheckBox method to happen — good guess, I think you're right. (Those reflection-based `Object` arguments are dangerous!) – bobince Jan 29 '10 at 23:25