0

i am using mvc 5 with razor view to develop something.i am using partial view to show the data on search button. i need to show checkbox with every row in the table i.e in foreach statement in the view.

the code i tried -

<input type="checkbox" name="@item.KidFeeId" id = "@item.KidFeeId"  value="@item.KidFeeId" />

@Html.CheckBoxFor(modelitem=> true,item.KidFeeId)    

@Html.CheckBox("KidFeeId", new { value = item.KidFeeId }) 

@Html.CheckBox("checkname") 

but any of the above code is unable to display the checkbox in browser.its blank space in the browser but on inspect the html it shows the code but not render it. can some one help me with the solution.

  • Strange..just post the html generated.. – Kartikeya Khosla Dec 06 '14 at 05:37
  • from where this hidden field come from???? and checkbox and hidden field has same name attribute which can create problem...plz post some more code you are using... – Kartikeya Khosla Dec 06 '14 at 05:42
  • hidden field generates automatically in browser. can you plz tell me your code to generate checkbox in foreach statement. – user3644725 Dec 06 '14 at 05:47
  • Your question is not clear. If you are asking why the ` is rendered in addition to the ` then it because that what's the `@Html.CheckBoxFor()` and `@Html.CheckBox()` methods do. Since unchecked checkboxes do not post back, the hidden input ensure a value does post back. Ans if your rendering controls in a `foreach` loop **then don't**. You need to use an `EditorTemplate` or a `for` loop so the controls are correctly named with indexers so they can post back to your model. –  Dec 06 '14 at 07:06
  • thanks @Stephen Muecke for this but can you show me some example so dat i can implement. – user3644725 Dec 06 '14 at 07:18
  • A checkbox if for binding to a `boolean` property but you seem to be trying to bind to an `int` (`KidFeeId`) so its hard to understand what your doing. [This answer](http://stackoverflow.com/questions/25923356/asp-net-mvc-complex-view-mapping/25923877#25923877) gives an example of using an `EditorTemplate` and [this one](http://stackoverflow.com/questions/26403028/umbraco-create-checkboxlist-property-with-prevalues-from-mvc-model/26404162#26404162) is an example of using a `for` loop –  Dec 06 '14 at 07:27
  • i need to use this checkbox for a bool value bt i was trying to show checkbox first that why i bind it with primary key of table – user3644725 Dec 06 '14 at 07:30
  • @user3644725 , post the code you have done with foreach loop , there's something wrong with that only – Vishal Sharma Dec 06 '14 at 13:42
  • Are you using any theme? Theme's CSS can set the opacity of checkbox to 0 for hiding default checkbox. I faced same problem by using a theme. – Ibrahim Khan Dec 06 '14 at 13:43

0 Answers0