javascript - MVC View foreach list with tag-it (SO style tagging) -
i populating table in mvc view check boxes , looking use tag-it same effect so.
just wondering if there out there know how incorporate foreach method tag-it when tag typed first populates whatever comes list , second makes "selected" if chosen typed out field.
<script type="text/javascript"> $(document).ready(function() { $("#mytags").tagit(); }); </script> <ul id="mytags"> <!-- existing list items pre-added tags --> <li>tag1</li> <li>tag2</li> </ul>
view:
<div class="form-group"> <div class="col-md-offset-2 col-md-10"> <table> <tr> @{ int cnt = 0; list<myblogger.models.tag> tag = viewbag.tags; foreach (var tags in tag) { if (cnt++ % 3 == 0) { @:</tr><tr> } @:<td> <input type="checkbox" name="selectedtags" value="@tags.id" @(html.raw(tags.assigned? "checked=\"checked\"" : "")) /> @tags.id @: @tags.name @:</td> } @:</tr> } </table> </div> </div>
in view:
<script type="text/javascript"> $(document).ready(function() { $("#mytags").tagit({ availabletags: @viewbag.alltags }); }); </script> <ul id="mytags"> @foreach (var tag in (viewbag.selectedtags list<myblogger.models.tag>)) { <li>@tag.name</li> } </ul>
in controller:
javascriptserializer serializer = new system.web.script.serialization.javascriptserializer(); viewbag.alltags = serializer.serialize(context.tags.toarray()); viewbag.selectedtags = context.tags.where(w => w.selected).tolist();
assuming using ef's context , have way populate list of tags tags should selected.