Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference in input type=Checkbox, @HTML.CheckBox and @HTML.CheckBoxFor?

I m new to MVC and confused what is difference in <Input type="Checkbox">, @HTML.CheckBox and @HTML.CheckBoxFor.

Can you please guide why two helpers are provided for same thing ? In which situation which one should be used ?

Thanks

Edit: Added Input type=checkbox

like image 229
Toubi Avatar asked Jan 03 '14 03:01

Toubi


1 Answers

<Input type="Checkbox"> is Html markup for a checkbox and @Html.CheckBox & @HTML.CheckBoxFor are Html Helpers for Razor view engine..

suppose your viewmodel has a property Person.HadDinner, then usually for model binding to work properly you will have to name the checkbox Person.HadDinner and id as Person_HadDinner..

you can use @Html.CheckBox like

@HTML.CheckBox("Person.HadDinner", Model.Person.HadDinner)

but if you are using @HTML.CheckBoxFor, it will be strongly typed..

@HTML.CheckBoxFor(x => x.Person.HadDinner)

in both the cases, final output markup will be

<input type="checkbox" id="Person_HadDinner" name="Person.HadDinner">
like image 190
dotNETbeginner Avatar answered Oct 28 '22 01:10

dotNETbeginner