asp.net mvc - Html Helper Check Box "Display Image" -
so not super familiar html helpers or razor syntax. im sure there way trying two, im not entirely sure. easier via controller, wanted give go in view. here's code:
<img src="@url.content(model.photo)" alt="@model.alternatetext" />
so displays images i'd them e displayed. wanted add box user check or uncheck, make image visible or not. there way somehow integrate html checkbox helper img? along these lines suppose...
@html.checkbox("photos", "show image") <img src="@url.content(model.photo)" alt="@model.alternatetext" />
thanks!
what have work far html part of equation goes, though syntax html.checkbox
off. second parameter boolean, indicating whether should checked or not. if want "show image" appear next it, you'll need put text in view , preferably wrap whole shebang label:
<label>@html.checkbox("photos", false) show image</label>
now, far showing , hiding image, you'll need use javascript that. i've give jquery version since it's simpler , comes mvc default:
<script> $(document).ready(function () { $('#photos').on('click', function () { if ($(this).is(':checked')) { $('#image').show(); } else { $('#image').hide(); } }); }); </script>
note: based on passing first parameter of "photos" html.checkbox
, resulting input have both name , id attributes of "photos" allowing select id. if change value, you'll need update js accordingly. also, code assumes adding id of "image" img tag. can use whatever want, again, update js accordingly.
Comments
Post a Comment